-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
[clang] Failure when upcasting to a base class in trailing return type expression #114024
Comments
@llvm/issue-subscribers-clang-frontend Author: Carlos Galvez (carlosgalvezp)
Hi,
Consider this example code: struct Base
{};
int foo(Base&);
struct Derived : Base
{
auto f() & -> decltype(foo(static_cast<Base&>(*this)))
{
return foo(static_cast<Base&>(*this));
}
}; Clang emits this failure: <source>:9:32: error: non-const lvalue reference to type 'Base' cannot bind to a value of unrelated type 'Derived'
9 | auto f() & -> decltype(foo(static_cast<Base&>(*this)))
| However, GCC accepts the code. I am confused as to why Clang complains; a Derived class should be possible to upcast to its base? Thanks! |
@carlosgalvezp auto f() & -> auto |
|
The return type is a not a complete-class context. |
You do not need to cast from
|
Thanks, that works! Still it would be good if we could keep an exact copy of the return expression into the return type, which is a common idiom. Some people even do this via a macro to ensure the 3 places where it's needed receive the same expression. Isn't this still a Clang issue given that all other major compilers accept the code? |
From what I can tell the other compilers are not standard-conforming and Clang behaves according to the standard. So fixing this would probably require a proposal to change the standard. (But I am just an interested user, so wait for some authoritative answer.) |
Ah, I see. Do you know in particular which section of the standard is relevant in this case? |
The implicit cast (which is also used by A class is complete only after its closing |
Thank you for the thorough explanation! |
Hi,
Consider this example code:
Clang emits this failure:
Repro.
However, GCC, MSVC and EDG accept the code. I am confused as to why Clang complains; a Derived class should be possible to upcast to its base?
Thanks!
The text was updated successfully, but these errors were encountered: