#include <iostream>
#include <type_traits>
using namespace std;
class MyClass
{
public:
int a = 1;
};
class Derived : public MyClass
{
public:
int b = 2;
};
class NotDerived
{
public:
int b = 3;
};
template<typename T, typename std::enable_if<std::is_base_of<MyClass, T>::value>::type* = nullptr>
T Foo(T bar)
{
return T();
}
int main() {
Derived d;
NotDerived nd;
std::cout << Foo(d).b << std::endl;; // works
//std::cout << (Foo(nd)).b << std::endl;; //compiler error
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dHlwZV90cmFpdHM+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBNeUNsYXNzCnsKCXB1YmxpYzoKCWludCBhID0gMTsKfTsKCmNsYXNzIERlcml2ZWQgOiBwdWJsaWMgTXlDbGFzcwp7CglwdWJsaWM6CglpbnQgYiA9IDI7Cn07CgpjbGFzcyBOb3REZXJpdmVkCnsKCXB1YmxpYzoKCWludCBiID0gMzsKfTsKCnRlbXBsYXRlPHR5cGVuYW1lIFQsIHR5cGVuYW1lIHN0ZDo6ZW5hYmxlX2lmPHN0ZDo6aXNfYmFzZV9vZjxNeUNsYXNzLCBUPjo6dmFsdWU+Ojp0eXBlKiA9IG51bGxwdHI+CiAgICBUIEZvbyhUIGJhcikKICAgIHsKICAgICAgIHJldHVybiBUKCk7CiAgICB9CgppbnQgbWFpbigpIHsKCURlcml2ZWQgZDsKCU5vdERlcml2ZWQgbmQ7CglzdGQ6OmNvdXQgPDwgRm9vKGQpLmIgPDwgc3RkOjplbmRsOzsgLy8gd29ya3MKCS8vc3RkOjpjb3V0IDw8IChGb28obmQpKS5iIDw8IHN0ZDo6ZW5kbDs7IC8vY29tcGlsZXIgZXJyb3IKCQoJcmV0dXJuIDA7Cn0=