Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler can't deduce the return type?

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?

like image 634
DarthRubik Avatar asked Mar 13 '18 22:03

DarthRubik


3 Answers

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());
like image 66
llllllllll Avatar answered Nov 11 '22 15:11

llllllllll


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());
};
like image 24
max66 Avatar answered Nov 11 '22 13:11

max66


@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.

like image 41
Arne Vogel Avatar answered Nov 11 '22 14:11

Arne Vogel