ARTICLE AD BOX
I have the below code. HasLogController<> returns true/false if the provided class has the member m_logController. But how to achieve it with this? The compiler always knows what's the class object of this. Currently I somehow can't figure out the type. I get the name but this is not what the template expects.
Please advise.
#include <iostream> template<typename T> struct HasLogController { struct Fallback { int m_logController; }; // introduce member name "m_logController" struct Derived : T, Fallback { }; template<typename C, C> struct ChT; template<typename C> static char (&f(ChT<int Fallback::*, &C::m_logController>*))[1]; template<typename C> static char (&f(...))[2]; static bool const value = sizeof(f<Derived>(0)) == 2; }; class TestClass { int m_logController; public: auto checkExistence() -> void; }; class ProgramHeap { public: int m_logController; }; ProgramHeap programHeap; auto TestClass::checkExistence() -> void { std::cout << HasLogController<ProgramHeap>::value << std::endl; // true std::cout << HasLogController<something-with-this>::value << std::endl; // ^^^^^^^^^^^^^^^^^^^ it should return true // because "this/TestClass" has m_logController } int main() { TestClass testClass; testClass.checkExistence(); }