Type "i" below in main (). Why is it int &?

I know that the type i below in main() is int& . That is why it must be initialized.

 int& f(){ static int i = 1; return i; } int main() { decltype(f()) i = f(); } 

But using paragraph 5p5 in the standard, I conclude that the expression f() is of type int as you delete the link.

From 7.1.6.2p4 , how can I say that the expression f() is an lvalue, given that the link has been removed from the return function?

+6
source share
1 answer

This reference is discarded when evaluating the type of expression (according to ยง 5/5), but this does not alter the fact that the expression for calling the function f() is an lvalue value. In clause 5.2.2 / 10 of the C ++ 11 standard:

A function call is an lvalue if the result type is a lvalue reference type or an rvalue reference to a type function, xvalue if the result type is an rvalue reference to an object type and otherwise a prvalue value.

In other words, the link is not discarded from the return type of the function itself, only from the type of the call expression of the function being evaluated (which, therefore, is int ).

The fact that a function returns an lvalue reference is what allows the type system to classify the corresponding function call expressions as lvalues, which in turn allows decltype add an lvalue reference to the type of the expression, thus giving int& .

+6
source

Source: https://habr.com/ru/post/945942/


All Articles