![](/img/trans.png)
[英]error C2908: explicit specialization; '' has already been instantiated
[英]Compiler Error C2766 : “explicit specialization; 'specialization' has already been defined” when using boost::disable_if
我正在尝试建立一个模板类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 { ... };
它将包含一个内部类At和一个static const int value
,该static const int value
指示模板参数的索引(S0为0,S1为1,依此类推)。 不久,它应该满足以下条件:
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) );
我试过使用boost::disable_if
,如下所示:
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
但是当我为S3定义专门化并且两个S2,S3属于相同类型aux::EmptyType
(或:我为S2定义专门化而两个S1,S2属于相同类型)会导致错误。
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> ]
任何想法如何解决问题? 如果我想size_t at<S0>()
处给size_t at<S1>()
方法size_t at<S0>()
0, size_t at<S1>()
处给size_t at<S1>()
方法size_t at<S0>()
1 ...?
请询问您是否需要更多信息。
假设boost :: is_same :: value返回0或1(如果您的布尔使用不同的值,只需编写一个小的编译时转换器),这是一个更简单的解决方案:用替换当前的At
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
};
};
如果需要更大的范围,则其值为十进制位掩码,请随时使用其他值。
由于这个答案,我设法部分地回答了第二个问题(关于模板功能):
#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
我也回答了我的第一个问题。 实际上,我想起来更容易:
// 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.