Compiler can't deduce the return type?

后端 未结 3 578
夕颜
夕颜 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:39

    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());
    

提交回复
热议问题