c++ - Declaring member or not depending on template parameter -
is possible declare or not member variable depending on template condition without using dummy empty type?
example:
struct empty{}; struct real_type{}; template<bool condition> struct foo { typename std::conditional<condition, real_type, empty>::type _member; };
you can derive template has specialization:
struct real_type { }; template<bool c> struct foo_base { }; template<> struct foo_base<true> { real_type _member; }; template<bool condition> struct foo : foo_base<condition> { };
as small test:
int main() { foo<true> t; t._member.x = 42; // ok foo<false> f; f._member.x = 42; // error! no _member exists }
Comments
Post a Comment