template overloading and SFINAE working only with

2019-01-23 14:06发布

问题:

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.

回答1:

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).



回答2:

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