Can I have a class that either has or not has member based on template? Imaginary code:
template <typename HasBazMember=true>
class Foo {
int bar;
ConditionallyHaveMember<int, HasBazMember> baz;
};
So in above I want Foo to have member “baz” and Foo not.
Yes, you can, but you have to specialize the entire class. Example:
If you can separate the logic, you can put those members in a base class so that you don’t need to duplicate the code for the entire class. For instance,
Or using Boost.CompressedPair: