Type condition in C ++ template class task

Using GCC 4.2. I have this meta label for a conditional type:

template <bool condition, typename Then, typename Else> struct IF { typedef Then RET; }; template <class Then, class Else> struct IF<false, Then, Else> { typedef Else RET; }; 

and when I use it as follows:

 template <typename T> class Param { IF< sizeof(int)<sizeof(long), long, int>::RET i; }; 

it works, but when I use it like this (trying to use the template options):

 template <typename T> class Param { IF< sizeof(int)<sizeof(long), T&, T* >::RET mParam; }; 

I get this error code:

 error: type 'IF<false, T&, T*>' is not derived from type 'Param<T>' 

Why is this happening? How to solve it? Thanks in advance!

+4
source share
1 answer

In the second case, that RET , depends on the type of pattern T The compiler must be sure that it will be a type in all possible instances (and cannot be a static member of some IF instance). You do this with the typename keyword.

 template <typename T> class Param { typename IF< sizeof(int)<sizeof(long), T&, T* >::RET mParam; }; 
+8
source

All Articles