decltype(std)是否合法,是否有任何目的?

Rya*_*ing 9 c++ g++ decltype c++11

decltype在命名空间周围使用时,我可以编写编译的代码,但该语句在g ++ 4.9.1下似乎没有任何影响,在它生成的clang下error: unexpected namespace name 'std': expected expression

例如,以下所有都在g ++下编译,但程序集不显示为它们生成的任何代码.

using s = decltype(std);
auto n = typeid(decltype(std)).name();
auto sz = n.size();
std::printf("size is %zu\n", sz+1);
std::printf("this type is: %s\n\n", n.c_str());

// the only limit is your imagination
int f();
std::ostream trash = f(typeid(decltype(std)) * 10 - 6 ^ typeid(decltype(std)));
Run Code Online (Sandbox Code Playgroud)

如果g ++允许这样做是对的吗?如果是这样,代码的优势是什么消失而不是导致编译时错误?

jro*_*rok 13

不,这不合法.语法允许的两种decltype-specifier形式是(N3690,§7.1.6.2/ 1):

decltype说明符:
decltype(表达式)
decltype ( auto )

并且命名空间名称不是表达式.

我引用的段落来自C++ 11的标准草案,因此decltype(auto)不适用于C++ 11.答案是一样的.