Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array and Rvalue

$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?

like image 797
Chubsdad Avatar asked Sep 07 '10 07:09

Chubsdad


1 Answers

I'm not sure what you refer to by "initialization/declaration" in this context. In the following, the array is a prvalue

template<typename T> using alias = T;

int main() { return alias<int[]>{1, 2, 3}[0]; }

This can be verified by decltype(alias<int[]>{1, 2, 3}) having the type int[3]. 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.

like image 175
Johannes Schaub - litb Avatar answered Oct 23 '22 09:10

Johannes Schaub - litb