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?
@liliscent has already explained the stages of compiling your example, but here is an additional reductio ad absurdum: In a method body, you can use identifiers from the same class that are declared after the method, because the body is only translated after parsing the full class definition. Imagine now that the deduced type of
foo
was available inside the definition ofThing
. Then we should be able to legally write the following:And what should
type_t
be now? Likewise, the following is not allowed:This fails, not because
bar
was unknown at the point where the definition offoo
takes effect, but because its return type has not been deduced yet.Now, while your example is unambiguous in theory, it would probably take a lot of effort to come up with standard language that allows your example while at the same time being narrow enough to forbid both of my examples. And then again, the benefit seems marginal at best.
It's because a
using
inside a class or struct see the declaration but not the definition of members. So seeauto
but doesn't seereturn 12;
.If different, would be dangerous because the definition of members can use the defined (
using
ortypedef
) types.Imagine something as follows
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
function
foo()
's body has not yet been analyzed.As a remedy, you can use
NOTE:
This phenomenon is only for class. The following code outside a class will compile: