【发布时间】:2017-02-21 09:11:47
【问题描述】:
我发现它们是不同的,语言标准规定了每个语句应该检索哪种类型(变量和表达式之间的差异)。但是我真的很想知道为什么这两种类型应该不同?
#include<stdio.h>
int x=0;
decltype((x)) y=x;
int main()
{
y=2;
printf("%d,",x);
decltype((1+2))&z=x;//OK (1+2) is an express, but why decltype should differ?
z=3;
printf("%d\n",x);
return 0;
}
运行结果是'2,3'
那么为什么decltype((int)) 被设计成int&,这里C++ 语言设计的考虑是什么?任何需要这种设计的语法一致性? (我不希望得到“这是设计使然”)
感谢您的解释。
【问题讨论】:
-
@some 我没有看到解释
-
@JohannesSchaub-litb 添加了带引号的答案
-
我认为你最好的机会是阅读“decltype”的建议。谷歌搜索“decltype wg21 proposal”给出了一些有趣的论文。
标签: c++11 variables types expression decltype