#include <iostream>
template<class Derived>
class CRTP {
protected:
Derived * derived_this() {
return static_cast<Derived *>(this);
}
};
template<class Derived>
struct Parent : public CRTP<Derived> {
private:
using CRTP<Derived>::derived_this;
public:
int y() {
return derived_this()->x();
}
};
struct Child : public Parent<Child> {
int x() {
return 5;
}
int z() {
return y();
}
};
int main() {
std::cout << Child().z() << std::endl;
return 0;
}
ICAgICNpbmNsdWRlIDxpb3N0cmVhbT4KCiAgICB0ZW1wbGF0ZTxjbGFzcyBEZXJpdmVkPgogICAgY2xhc3MgQ1JUUCB7CiAgICBwcm90ZWN0ZWQ6CiAgICAgIERlcml2ZWQgKiBkZXJpdmVkX3RoaXMoKSB7CiAgICAgICAgcmV0dXJuIHN0YXRpY19jYXN0PERlcml2ZWQgKj4odGhpcyk7CiAgICAgIH0KICAgIH07CiAKICAgIHRlbXBsYXRlPGNsYXNzIERlcml2ZWQ+CiAgICBzdHJ1Y3QgUGFyZW50IDogcHVibGljIENSVFA8RGVyaXZlZD4gewogICAgcHJpdmF0ZToKICAgICAgdXNpbmcgQ1JUUDxEZXJpdmVkPjo6ZGVyaXZlZF90aGlzOwogICAgcHVibGljOgogICAgICBpbnQgeSgpIHsKICAgICAgICByZXR1cm4gZGVyaXZlZF90aGlzKCktPngoKTsKICAgICAgfQogICAgfTsKIAogICAgc3RydWN0IENoaWxkIDogcHVibGljIFBhcmVudDxDaGlsZD4gewogICAgICBpbnQgeCgpIHsKICAgICAgICByZXR1cm4gNTsKICAgICAgfQogICAgICBpbnQgeigpIHsKICAgICAgICByZXR1cm4geSgpOwogICAgICB9CiAgICB9OwogCiAgICBpbnQgbWFpbigpIHsKICAgICAgc3RkOjpjb3V0IDw8IENoaWxkKCkueigpIDw8IHN0ZDo6ZW5kbDsKICAgICAgcmV0dXJuIDA7CiAgICB9Cg==