下面的程序试图为用户定义类型
编译器输出:
foo
提供 std::tuple_element
的特化。不幸的是,在 libc++ 中,clang-3.5
拒绝了它,但在其他编译器或者使用其他标准库和 clang-3.5
一起使用时,程序被接受了。这是否正确?如果不是,请说明原因。#include <utility>
struct foo {};
namespace std
{
template<size_t, class> struct tuple_element;
template<size_t i>
struct tuple_element<i, foo> {};
}
int main()
{
return 0;
}
编译器输出:
$ clang-3.5 -std=c++11 -stdlib=libc++ -lc++ test.cpp
test.cpp:11:8: error: explicit specialization of non-template struct 'tuple_element'
struct tuple_element<i, foo> {};
^ ~~~~~~~~
1 error generated.
$ clang-3.5 -std=c++11 -lstdc++ test.cpp
(no error)
$ g++-4.9 -std=c++11 test.cpp
(no error)
struct
替换为class
也可以修复一个警告。另外请注意,前向声明会使程序不合法,因为您可以在std
中特化类型,但任何声明都是禁止的(即使是前向声明!)。构建中断是符合规范的。 - Yakk - Adam Nevraumontnamespace std
添加了一个声明。因此,行为是未定义的。 - Yakk - Adam Nevraumont