Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template overloading and SFINAE working only with functions but not classes

can someone explain why the compiler accepts only this code

template<typename L, size_t offset, typename enable_if< (offset<sizeof(L)), int >::type =0>
void a_function(){}

template<typename L, size_t offset, typename enable_if< (offset==sizeof(L)), int >::type =0>
void a_function(){}

but not this:

template<typename L, size_t offset, typename enable_if< (offset<sizeof(L)), int >::type =0>
class a_class{};

template<typename L, size_t offset, typename enable_if< (offset==sizeof(L)), int >::type =0>
class a_class{};

The compiler sees the second class template as a redefinition of the first.

like image 287
Lorenzo Pistone Avatar asked Jan 31 '12 12:01

Lorenzo Pistone


2 Answers

You have to use specialization for classes. Typically, it is done with an extra parameter:

template <class P, class dummy = void>
class T;

template <class P>
class T<P, typename enable_if<something, void>::type> {
   the real thing
};

Two class (or class template) declarations with the same name should always declare the same class or class template (or be a specialization, in which case it is still the same template).

like image 95
jpalecek Avatar answered Oct 21 '22 00:10

jpalecek


You probably want to do something like this (ideone.com link):

#include <iostream>

template< typename PL, size_t pOffset, int pC = int( sizeof(PL) ) - int( pOffset ) >= 0 ? ( int( sizeof(PL) ) - int( pOffset ) == 0 ? 0 : 1 ) : -1 >
class TClass
{
};

template< typename PL, size_t pOffset >
class TClass< PL, pOffset, -1 >
{
 public:
  static int const sI = -1;
};

template< typename PL, size_t pOffset >
class TClass< PL, pOffset, 0 >
{
 public:
  static int const sI = 0;
};

template< typename PL, size_t pOffset >
class TClass< PL, pOffset, 1 >
{
 public:
  static int const sI = 1;
};

int main(void )
{
 TClass< char, 0 > lC0;
 TClass< char, 1 > lC1;
 TClass< char, 2 > lC2;

 std::cout << lC0.sI << " : " << lC1.sI << " : " << lC2.sI << std::endl;

 return ( 0 );
}

Program output:

1 : 0 : -1
like image 28
lapk Avatar answered Oct 21 '22 00:10

lapk