So what I want to do is to create a template class which may or may not contain a member variable based on the template argument passed in. like following:
t
I have a workaround for this. Probably looks ugly but does resolve some of my issues
First I define a type with zero size:
typedef int zero[0];
Next I create a macro:
#ifndef VAR
#define VAR(Enable,YourType,VarName) \
std::conditional< Enable,YourType,zero >::type VarName
#endif
Then a class like this:
template < int Cond >
class Foo
{
VAR(Cond == 0,int,var);
void print()
{
if (!sizeof(var)) return;
//...
}
};
When you are using a result like var
, check the size of it before using it. If the size is zero, it is invalid.