Using & # 8594; operator on arrays

I just accidentally discovered this weird syntax. You can actually use operator-> on arrays.

struct Foo
{
    void bar() { }
};

int main()
{
    Foo foo[3] = { Foo(), Foo(), Foo() };
    foo->bar();
}

      

I thought that operator -> was only for pointers to objects, and I don't see pointers in this code. Even if there were pointers, they would be dangling pointers due to Foo's temporary times. What makes this possible?

+3


source to share


5 answers


Arrays can be decomposed into pointers, and they point to the first element of the array. So, for example, you can do this:

void bar(int*) {}

int a[42] = {};
int* b = a;      // a decayed to int*, got assigned to b
bar(a);          // a decayed to int* to match bar parameter type

      

In your example, foo

decays to a pointer to the first element of the array foo

when the operator is applied ->

. The following two are equivalent:

foo->bar();
(&foo[0])->bar();

      



As for the validity of the object it points to, an array such as

Foo foo[3];

      

always contains 3 foo

default initialized objects . What you did is initialize them explicitly, but that doesn't change anything. The array contains copies of the objects that you used to initialize your elements.

+5


source


You can actually use operator->

for arrays.

Yes, the standard conversion between arrays and pointers means that an expression of an array type, for example foo

, can decay to a pointer to the first element of the array if used in the context where the pointer is expected.

I thought operator-> was only for pointers to objects



It is for any type that converts to an object pointer - including an array - and for any class type that it overloads.

Even if there were pointers, they would be dangling pointers due to temporary foo

.

No, foo

it is not temporary, and none of its elements contain. You initialized them by copying temporary files, but the copies in the array continue as long as the array itself.

+3


source


I thought that operator -> was only for pointers to objects, and I don't see pointers in this code.

The array decays to a pointer when used in an expression like yours.

Even if there were pointers, they would be dangling pointers due to Foo's temporary times. What makes this possible?

Line

 Foo foo[3] = { Foo(), Foo(), Foo() }

      

fills in with foo

three valid objects, not temporary ones.

+2


source


http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3820.html

ยง 4.2 Converting an array to a pointer [conv.array]

1 An lvalue or rvalue of type, array of N 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.

+2


source


You are declaring an array of three instances Foo

. You are initializing the array by creating 3 instances with { Foo(), Foo(), Foo() }

and they are not moved or copied to a place in the array, they are actually created there, so there are no temporary files.

+2


source







All Articles