首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用boost::disable_if时出现编译器错误C2766:"explicit specialization;'specialization‘已定义“

使用boost::disable_if时出现编译器错误C2766:"explicit specialization;'specialization‘已定义“
EN

Stack Overflow用户
提问于 2012-01-19 18:17:05
回答 3查看 2.5K关注 0票数 2

我正在尝试构建一个模板类Fod

代码语言:javascript
运行
复制
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 { ... };

它将包含一个内部类,其中的static const int value表示模板参数的索引(0表示S0,1表示S1,依此类推)。简而言之,它应该满足以下条件:

代码语言:javascript
运行
复制
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,如下所示:

代码语言:javascript
运行
复制
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都是相同类型)时,它会导致错误。

代码语言:javascript
运行
复制
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>()给0,让size_t at<S1>()给1…?

如果您需要更多信息,请询问。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-19 23:30:57

有一个更简单的解决方案,假设boost::is_same::value返回0或1(如果您的bool使用不同的值,只需编写一个小的编译时转换器):将当前的At替换为

代码语言:javascript
运行
复制
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
    };
};

它的计算结果是一个十进制位掩码,如果您需要更大的范围,请随意使用其他值。

票数 1
EN

Stack Overflow用户

发布于 2012-01-19 22:02:08

我成功地回答了第二个问题(关于模板函数),这部分要归功于this answer

代码语言:javascript
运行
复制
#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
票数 0
EN

Stack Overflow用户

发布于 2012-01-19 22:58:12

我也回答了我的第一个问题。实际上,这比我想的要简单得多:

代码语言:javascript
运行
复制
// 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8924325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档