Compiler Error C2766 : “explicit specialization; 'specialization' has already been defined” when using boost::disable_if

StackOverflow https://stackoverflow.com/questions/8928189

Question

I'm trying to build a template class Fod

template<typename S0 = aux::EmptyType, typename S1 = aux::EmptyType, typename S2 = aux::EmptyType, typename S3 = aux::EmptyType, typename S4 = aux::EmptyType, typename S5 = aux::EmptyType, typename S6 = aux::EmptyType, typename S7 = aux::EmptyType, typename S8 = aux::EmptyType, typename S9 = aux::EmptyType>
class Fod { ... };

which will contain an inner class At with a static const int value indicating the index of the template argument (0 for S0, 1 for S1 and so on). Shortly, it should satisfy the condition:

struct Type0 {}; struct Type1 {};
BOOST_STATIC_ASSERT( (Fod<Type0>::At<Type0>::value == 0) );
BOOST_STATIC_ASSERT( (Fod<Type0, Type1>::At<Type0>::value == 0) );
BOOST_STATIC_ASSERT( (Fod<Type0, Type1>::At<Type1>::value == 1) );

I've tried using boost::disable_if as follows:

template<class T, class Enable = void>
class At; // undefined

template<>
struct At<S0, typename boost::disable_if<boost::is_same<S0, aux::EmptyType> >::type > {
    static const int value = 0;
};

template<>
struct At<S1, typename boost::disable_if<boost::is_same<S1, aux::EmptyType> >::type > {
    static const int value = 1;
};

template<>
struct At<S2, typename boost::disable_if<boost::is_same<S2, aux::EmptyType> >::type > {
    static const int value = 2;
};

template<>
struct At<S3, typename boost::disable_if<boost::is_same<S3, aux::EmptyType> >::type > {
    static const int value = 3;
};

// and so on for S4...S9

but it results in error when I define specialization for S3 and both S2,S3 are of the same type aux::EmptyType (or: I define specialization for S2 and both S1,S2 are of the same type).

4>C:\phd\cpp\src\boost/dst/fod.hpp(144): error C2766: explicit specialization ; 'boost::dst::fod<S0>::At<boost::dst::aux::EmptyType,boost::mpl::s_item<T,Base>>' has already been defined
4>          with
4>          [
4>              S0=Type0
4>          ]
4>          and
4>          [
4>              T=Type0,
4>              Base=boost::mpl::set0<>::item_
4>          ]

Any ideas how to solve the problem? And if I wanted a method size_t at<S0>() to give 0, size_t at<S1>() to give 1...?

Please ask if you need more information.

Was it helpful?

Solution

There's an easier solution to this, assuming that boost::is_same::value returns 0 or 1 (if your bool uses different values, just write a small compile-time converter): Replace your current At with

template <typename T>
struct At {
    enum {
        value = 
        boost::is_same<T, S0>::value) + 
        boost::is_same<T, S1>::value * 10 + 
        boost::is_same<T, S2>::value * 100
    };
};

which evaluates to a decimal-bitmask, if you need a bigger range feel free to use other values.

OTHER TIPS

I've managed to answer the second question (about a template function) partially thanks to this answer:

#include <boost/utility/enable_if.hpp>

template <typename T>
static int at(typename boost::enable_if_c< boost::is_same<T, S0>::value && !boost::is_same<T, aux::EmptyType>::value, T >::type = T())
{
    return 0;
}
// and so on for each class template parameter S1,...,S9

I answered my first question as well. Actually, it was easier that I had thought:

// main general template (unused or throws a compilation error)
template<class T, class Enable = void >
struct At {};

template<typename T>
struct At<T, typename boost::enable_if< boost::is_same<T, S0> >::type >
{
    static const int value = 0;
};
// and so on for each class template parameter S1,...,S9
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top