【问题标题】:Is decltype(std) legal, and does it have any purpose?decltype(std) 合法吗,它有什么用途吗?
【发布时间】:2014-09-30 15:26:35
【问题描述】:

在命名空间周围使用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)));

如果 g++ 允许这样做是正确的?如果是这样,代码消失而不是导致编译时错误有什么好处?

【问题讨论】:

  • 似乎是 g++ 中的一个错误。

标签: c++ c++11 g++ decltype


【解决方案1】:

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

decltype-specifier:
decltype ( 表达式 )
decltype ( auto )

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-12
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2011-08-07
    • 1970-01-01
    • 2014-10-06
    相关资源
    最近更新 更多