I am trying to use the decltype
keyword on an auto function:
struct Thing {
static auto foo() {
return 12;
}
using type_t =
It's because a using
inside a class or struct see the declaration but not the definition of members. So see auto
but doesn't see return 12;
.
If different, would be dangerous because the definition of members can use the defined (using
or typedef
) types.
Imagine something as follows
struct Thing {
static auto foo() {
return type_t{};
}
using type_t =
decltype(foo());
};
@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 of Thing
. Then we should be able to legally write the following:
struct Thing {
static auto foo() {
return type_t{};
}
using type_t =
decltype(foo());
};
And what should type_t
be now? Likewise, the following is not allowed:
struct Thing {
static auto foo() { return bar(); }
static auto bar() { return foo(); }
};
This fails, not because bar
was unknown at the point where the definition of foo
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.
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());