Array and Rvalue
Asked Answered
S

4

13

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

Scoutmaster answered 7/9, 2010 at 7:49 Comment(1)
possible duplicate of I think I may have come up with an example of rvalue of array typePublicness
R
19

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.

Racial answered 4/2, 2012 at 10:17 Comment(2)
To be more specific, in current wording, f().array is an xvalue isn't? What I think it is impossible to get is an array prvalue, isn't?Baize
@Peregring-lk yes it's an xvalue now (not at the time of the answer). And in alias<int[]>{1, 2, 3} the expression is both an xvalue and a prvalue. First it is a prvalue without an object associated, and when you index into it, the "materialization conversion" creates an object for it and gives the expression value-category "xvalue", before taking a pointer to the first element of the array.Racial
D
1

You cannot get an rvalue of array type. Arrays can only be lvalues, and whenever they are used in an lvalue they decay to a pointer to the first element.

int array[10];
int * p = array; // [1]

The expression array in [1] is an lvalue of type int (&)[10] that gets converted to an rvalue of type int *p, that is, the rvalue array of N==10 T==int is converted to an lvalue of type pointer to T==int.

Dismay answered 7/9, 2010 at 7:53 Comment(6)
Can you please show me an example where the above quote holds good for an Rvalue of Array type?Scoutmaster
Is that the reason why functions cannot return array types ?Isolating
@Cedric H.: In §8.3.5[dcl.fct]/3 the standard specifies that After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively. That is, the standard specifies that void foo( char [10] ) is a declaration exactly equivalent to void foo( char\* ) -- that is for arguments. Then in §8.3.5/6 it says: Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things. for return typesRheumatic
I downvoted this, because it's wrong. You can have rvalues of array type.Racial
@litb: can you provide an example with an rvalue expression of array type that does not decay into a pointer?Rheumatic
@David Rodríguez - dribeas Here: "12" - lvalue string, {'1', '2', '\0'} - rvalue char array. godbolt.org/z/lIcRbX Seems MSVC2017 even in 15.9.6 could not compile it correctly, but old gcc/clang - can!Roar
C
0

It's very easy to get array right reference value, for a class reference property will be added to its members.

struct s {
    int arr[5];
};
using array_right_reference = decltype((s().arr));
static_assert(std::is_same<array_right_reference, int (&&)[5]>::value, "");
Coumarin answered 26/3, 2023 at 4:43 Comment(0)
S
-2

Would this stand a chance to demonstrate Array Rvalue?

int main(){
 int buf[10][10];

 int (*p)[10] = buf;

 int (*p2)[10] = p;      // LValue to Rvalue conversion of Array type 'p'
}
Scoutmaster answered 7/9, 2010 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.