decltype(“Hello”)的结果是什么? [英] What is the result of decltype("Hello")?
问题描述
我从所有编译器得到意想不到的结果,我尝试以下(GCC 4.7.2,GCC 4.8.0测试版,ICC 13.0.1,Clang 3.2,VC10):
#include< type_traits>
int main()
{
//这将触发
static_assert(
std :: is_same< decltype(Hello),char const [6]> :: value,
Error!
);
}
我希望上面的编译时断言火,但它。毕竟,这个不(如预期):
#include< type_traits>
int main()
{
char const hello [6] =Hello;
//这不会触发
static_assert(
std :: is_same< decltype(hello),char const [6]> :: value,
错误!
);
}
那么 decltype(Hello )
根据C ++ 11标准(参考高度赞赏)?
[注意:最初,这不是一个自我回答的问题;
根据我的意见,我只是偶尔找到答案,到C ++ 11标准的附录C(2.14.5):
字符串文字的类型从array charto array of const char 。[....]
.6.2 / 4指定(关于 decltype
的结果):
表示为
decltype(e)
定义如下:
- 如果
code>是一个未加括号的id表达式或一个未加括号的类成员访问(5.2.5),
。如果没有这样的实体,或decltype(e)
是< c $ c> ee
命名一组重载的函数,程序是不成形的;
- 否则,如果
e
是xvalue,decltype(e)
是&
,其中T
是e
的类型;
- 否则,如果
e
是一个左值,decltype(e)
是T&
,其中T
是e
; c> c> c> > e 。
字符串文字是左值 ,根据上述段落和附件C的段落, code> decltype(Hello)是 一个
#include< type_traits>
int main()
{
//这将不会触发
static_assert(
std :: is_same< decltype(Hello),char const(&)[6]> :: value,
Error!
);
}
最后,即使 hello
variable 也是一个左值,问题文本中的第二个编译时断言不会触发,因为 hello
是一个未加括号的id表达式,这使得它落入上面列表的第一项从段落7.1.6.2/4。因此, decltype(hello)
的结果是由 hello
命名的实体的类型,它是 char const [6]
。
I'm getting unexpected results from all compilers on which I tried the following (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!"
);
}
I would have expected the compile-time assertion above not to fire, but it does. After all, this one does not (as expected):
#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!"
);
}
So what is the result of decltype("Hello")
according to the C++11 Standard (references are highly appreciated)? What should I compare it to so that the compile-time assertion above doesn't fire?
[Note: Originally, this was not meant to be a self-answered question; I just happened to find the answer myself while I was describing my attempts to investigate, and I thought it would have been nice to share it.]
According to Annex C (2.14.5) of the C++11 Standard:
The type of a string literal is changed from "array of char" to "array of const char." [....]
Moreover, Paragraph 7.1.6.2/4 specifies (about the result of decltype
):
The type denoted by
decltype(e)
is defined as follows:— if
e
is an unparenthesized id-expression or an unparenthesized class member access (5.2.5),decltype(e)
is the type of the entity named bye
. If there is no such entity, or ife
names a set of overloaded functions, the program is ill-formed;— otherwise, if
e
is an xvalue,decltype(e)
isT&&
, whereT
is the type ofe
;— otherwise, if
e
is an lvalue,decltype(e)
isT&
, whereT
is the type ofe
;— otherwise,
decltype(e)
is the type ofe
.
Since string literals are lvalues, according to the above Paragraph and the Paragraph from Annex C, the result of decltype("Hello")
is an lvalue reference to an array of size 6 of constant narrow characters:
#include <type_traits>
int main()
{
// This will NOT fire
static_assert(
std::is_same<decltype("Hello"), char const (&)[6]>::value,
"Error!"
);
}
Finally, even though the hello
variable is also an lvalue, the second compile-time assertion from the question's text does not fire, because hello
is an unparenthesized id-expression, which makes it fall into the first item of the above list from Paragraph 7.1.6.2/4. Therefore, the result of decltype(hello)
is the type of the entity named by hello
, which is char const[6]
.
这篇关于decltype(“Hello”)的结果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!