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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With