Open carlosgalvezp opened 3 days ago
@llvm/issue-subscribers-clang-frontend
Author: Carlos Galvez (carlosgalvezp)
@carlosgalvezp This way works:
auto f() & -> auto
@Rajveer100
decltype(auto)
is unfortunately not SFINAE-friendly, so I don't believe I can use that.
The return type is a not a complete-class context. Derived
is not complete yet at this point and static_cast
ignores inheritance-based casts if the derived class type is incomplete, even if the bases have been seen already.
You do not need to cast from this
though. The type and value category of the static_cast
expression doesn't depend on the argument and because you already know that the cast will be valid once the class is complete, there is no SFINAE implication specific to the cast either:
auto f() & -> decltype(foo(std::declval<Base&>()))
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 static_cast
) is specified in [conv.ptr]/3. It explicitly requires the derived type to be complete.
A class is complete only after its closing }
bracket (i.e. when it is "reachable", see definition in [module.reach])) per [class.mem.general]/8 except in complete-class contexts, which are listed in [class.mem.general]/7 and don't include the trailing return type.
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!