Compiler can't deduce the return type?

后端 未结 3 580
夕颜
夕颜 2021-01-04 08:41

I am trying to use the decltype keyword on an auto function:

struct Thing {
   static auto foo() {
     return 12;
   }
   using type_t =
               


        
3条回答
  •  不知归路
    2021-01-04 09:36

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

提交回复
热议问题