LandP
LandP

Reputation: 173

Why is the built-in array subscript can be an lvalue?

enter image description here

Indicate in cppreference that the expression representing the subscript must be a prvalue of unscoped enumeration or integral type.

So why can a for loop that traverses a built-in array, which beginners have learned, be compiled.

such as:

int a[10] = {0};
// as we know ,i is a lvalue
for(int i = 0; i < 10; i++)
{
    std::cout << a[i] << std:endl;
}

Upvotes: 1

Views: 154

Answers (1)

eerorika
eerorika

Reputation: 238351

Indicate in cppreference that the expression representing the subscript must be a prvalue of unscoped enumeration or integral type.

// as we know ,i is a lvalue

So why can a for loop that traverses a built-in array, which beginners have learned, be compiled.

A glvalue expression may be implicitly converted to prvalue with lvalue-to-rvalue conversion.

Upvotes: 2

Related Questions