数组和右值 [英] Array and Rvalue
问题描述
$ 8.4 / 1 - 左值或类型的右值
数组N个T或未知阵列
结合的T可被转换为一个
右值类型的指针到T的
结果是一个指向第一个
数组的元素
$4.2/1 - "An lvalue or rvalue of type "array ofN T" or "array of unknown bound of T" can be converted to an rvalue of type "pointer to T." The result is a pointer to the first element of the array."
我不知道我们如何得到比初始化/申报期间以外的数组类型的右值?
I am not sure how do we get an rvalue of an array type other than during initialization/declaration?
推荐答案
我不知道你在这方面是指什么是初始化/宣言。但数组类型的右值都不算少见。
I'm not sure what you refer to by "initialization/declaration" in this context. But rvalues of array type are not too uncommon.
struct data { int array[10]; };
data f();
int main() { return f().array[0]; }
现在 F()。数组
是数组类型的右值,它会被转换为指针。同为低于C ++ 11 code
Now f().array
is an rvalue of array type and it will be converted to a pointer. Same for the below C++11 code
template<typename T> using alias = T;
int main() { return alias<int[]>{1, 2, 3}[0]; }
创建阵列上飞这样不是最初打算工作,但溜进由这路对统一初始化相关工作的工作草案。当我意识到,在C ++ 0x的工作草案中的某些段落禁止这种对即时创建阵列临时的一些特殊情况,而其他几款允许的话,我发到C ++委员会的缺陷报告,然后根据上GCC的部分工作实现的决定,全力支持这一点。
Creating arrays this way on the fly wasn't initially intended to work but slipped into the working draft by-the-way of related work on uniform initialization. When I realized that some paragraphs in the C++0x working draft disallow some special case of this on-the-fly creation of array temporaries while other paragraphs allow it, I sent a defect report to the C++ committee, which then on the basis of GCC's partially working implementation decided to fully support this.
这篇关于数组和右值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!