Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dcl.type.auto.deduct] Make f1 example well-formed #7683

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Quuxplusone
Copy link
Contributor

@W-E-Brown reports that in the example in [dcl.type.auto.deduct], my P2266 "Simpler implicit move" introduced an example which, while correct in the point it's illustrating, is ill-formed for another reason.

auto f1(int x) -> decltype((x)) { return (x); }  // return type is int&
auto f2(int x) -> decltype(auto) { return (x); }  // return type is int&&

GCC rightly complains about f1:

error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
 8 | auto f1( int x ) -> decltype( (x) ) { return (x); }

That is, the Standard's example is correct that f1's deduced return type is int&, but then the function body is ill-formed because you can't bind that int& to the rvalue expression (x).

f1's function body exists at all here only to create a sort of tutorial parallelism to f2. We can discard that parallelism; then f1 becomes well-formed, while still illustrating the same point.


Question for discussion: Walter also points out that even though f2 is well-formed, it invariably returns a dangling reference to function parameter x. (GCC correctly detects this, and warns.) Is this a bad thing? If it is a bad thing, then can anyone think of a way to adjust the example so that it still illustrates something useful here, but doesn't have a dangling reference?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant