decltype(“Hello”)的结果是什么?

我收到了所有编译器的意外结果(GCC 4.7.2,GCC 4.8.0 beta,ICC 13.0.1,Clang 3.2,VC10):

#include <type_traits> int main() { // This will fire static_assert( std::is_same<decltype("Hello"), char const[6]>::value, "Error!" ); } 

我希望上面的编译时间断言不会触发,但它确实如此。 毕竟,这不是(如预期):

 #include <type_traits> int main() { char const hello[6] = "Hello"; // This will not fire static_assert( std::is_same<decltype(hello), char const[6]>::value, "Error!" ); } 

那么根据C ++ 11标准, decltype("Hello")的结果是什么(高度赞赏引用)? 我应该如何比较以便上面的编译时间断言不会触发?

[ 注:本来,这并不是一个自我回答的问题, 当我正在描述我的调查尝试的时候,我只是碰巧find了答案,我认为这将是很高兴分享。 ]

根据C ++ 11标准的附录C(2.14.5)

string文字的types从“char of array”改为“ const char数组 ”。[….]

此外,第7.1.6.2/4段规定(关于decltype的结果):

decltype(e)表示的types定义如下:

– 如果e是未经授权的idexpression式或未经授权的类成员访问(5.2.5),则decltype(e)是由e命名的实体的types。 如果没有这样的实体,或者如果e名称是一组重载的函数,则该程序是不合格的;

– 否则,如果e是一个x值,则decltype(e)T&& ,其中Te的types;

否则,如果e是左值,则decltype(e)T& ,其中Te的types ;

否则, decltype(e)decltype(e)的types。

由于string文字是 左值 ,根据上述段落和附录C中的段落, decltype("Hello")左值引用常量窄字符的大小为6的数组:

 #include <type_traits> int main() { // This will NOT fire static_assert( std::is_same<decltype("Hello"), char const (&)[6]>::value, "Error!" ); } 

最后,尽pipehellovariables也是一个左值,但是从问题文本开始的第二个编译时断言并不会被触发,因为hello是一个非隐含的idexpression式 ,这使得它成为了第7.1节中上面列表的第一项.6.2 / 4。 因此, decltype(hello)的结果是由hello命名的实体的types,它是char const[6]