I came across this code snipped involving trailing return types and inheritance.
The following minimal example compiles fine with g++, not with clang
struct Base {};
int foo(Base&) {
return 42;
}
struct Derived : public Base {
auto bar() -> decltype(foo(*this)) {
return foo(*this);
}
};
int main()
{
Derived derived;
derived.bar();
return 0;
}
However, if we change auto bar() -> decltype(foo(*this))
to decltype(auto) bar()
(c++14 extension), the code compiles also with clang. Link to godbolt https://godbolt.org/z/qf_k6X .
Can anyone explain me
- how
auto bar() -> decltype(return expression)
differs fromdecltype(auto) bar()
- why the behavior between compilers differs
- what the right implementation is?