首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GCC vs匿名类型作为模板参数

GCC vs匿名类型作为模板参数
EN

Stack Overflow用户
提问于 2011-03-14 00:40:45
回答 1查看 832关注 0票数 1

这段代码可以用VS (/za)编译,但不能用GCC编译。谁是对的,谁是错的?或者两者都是错的/对的?

代码语言:javascript
复制
#include <iostream>
#include <type_traits>
using namespace std;

struct NullType
{
    //NullType(){}
template<class T>
    NullType(T value){}
enum {value};
};

template<class T>
struct IntType
{
    typedef T type;

};

template<int value_>
struct Low
{
    typedef int type;
    enum {value = value_};
};

template< class T>
struct Low_impl
{
protected:
T value_;
Low_impl():value_(T()){/*e.b.*/}
Low_impl(T value):value_(value){/*e.b.*/}
};

template<class T>
struct isNullType
{
    enum {value = false};
};

template<>
struct isNullType<NullType>
{
    enum {value = true};
};

template<class T>
struct TypeTraits
{
    typedef T type;
};



/*template<>
struct TypeTraits<int>
{
    typedef int type;
};*/

template<class Int_>
struct Int_Type_Tag
{
    static_assert(std::is_integral<Int_>::type,"Non Integral Type Is ILLEGAL As a Parameter to this class ");
    typedef Int_ type;
};

template<class T>
struct TypeTraits<Int_Type_Tag<T>>
{
    typedef typename Int_Type_Tag<T>::type type;
};

template<class Int_Type,class L = NullType>
struct Int : private std::conditional<isNullType<L>::value,
                                      NullType,
                                      Low_impl<typename TypeTraits<Int_Type>::type>>::type
{
    typedef typename std::conditional<isNullType<L>::value,
                                      NullType,
                                      Low_impl<typename TypeTraits<Int_Type>::type>>::type BaseType;
Int():BaseType(L::value){}
};

int main()
{
    Int<int> a;
    cout << sizeof(a);
    return 0;
}  

来自GCC 4.5.1的错误

错误:对‘NullType::NullType(NullType::&)’的调用没有匹配的函数

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-14 01:48:05

您需要使用匿名枚举NullType::value的类型实例化NullType::NullType(T)

这是在gcc 4.5中实现的N2657所允许的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5290770

复制
相关文章

相似问题

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