I am trying to use the decltype
keyword on an auto function:
struct Thing {
static auto foo() {
return 12;
}
using type_t =
decltype(foo());
};
And I get the following error (gcc 7.4):
<source>:6:25: error: use of 'static auto Thing::foo()' before deduction of 'auto'
decltype(foo());
^
<source>:6:25: error: use of 'static auto Thing::foo()' before deduction of 'auto'
Why has the compiler not yet deduced the return type?
Because for class definition, compiler will first determine all member names and types. Function body is analyzed after this is done.
That's why a class member function can call another member function declared after its own definition.
At the point compile is determining
using type_t = decltype(foo());
function foo()
's body has not yet been analyzed.
As a remedy, you can use
static auto foo() -> decltype(12) {
return 12;
}
NOTE:
This phenomenon is only for class. The following code outside a class will compile:
auto bar() { return 12; }
using t = decltype(bar());