首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >GCC是bug还是UB?这段代码应该编译吗?

GCC是bug还是UB?这段代码应该编译吗?
EN

Stack Overflow用户
提问于 2012-12-02 14:44:22
回答 1查看 722关注 0票数 16

下面的代码可以用clang编译得很好,但不能用GCC编译(尝试了4.1.2、4.5.4和4.7.2):

代码语言:javascript
复制
template <typename T>
struct A
{
    struct B { };
};

template <typename T>
bool operator==(typename A<T>::B const& b, T const&  t);

enum { BAR };

template <typename T>
bool test()
{
    return 0 == BAR;
}

来自GCC 4.7.2的错误消息是:

代码语言:javascript
复制
a.cpp: In instantiation of ‘struct A<<anonymous enum> >’:
a.cpp:12:6:   required by substitution of ‘template<class T> bool operator==(const typename A<T>::B&, const T&) [with T = <anonymous enum>]’
a.cpp:19:17:   required from here
a.cpp:6:12: error: ‘<anonymous enum>’ is/uses anonymous type
a.cpp:6:12: error:   trying to instantiate ‘template<class T> struct A<T>::B’
a.cpp:6:12: error: ‘<anonymous enum>’ is/uses anonymous type
a.cpp:6:12: error:   trying to instantiate ‘template<class T> struct A<T>::B’

GCC拒绝代码是正确的,还是我遇到了它的bug?

附注:我在尝试构建一个开源项目时看到了这个错误。我试着做一个尽可能小的例子来再现它。

EN

回答 1

Stack Overflow用户

发布于 2012-12-04 20:40:45

根据原标准,不是合法的C++:

标准的14.3.1说:

2局部类型、无链接类型、未命名类型或由这些类型组合而成的类型不能用作模板类型参数的模板参数。[示例:...

然而,我相信最新的C++11标准已经取消了这一限制。这可能解释了为什么一些编译器接受它,而另一些编译器拒绝它。

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

https://stackoverflow.com/questions/13667057

复制
相关文章

相似问题

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