Search code examples
c++c++11dryauto

Why can auto not be used as a return type for function declaration


This code does not compile (intel icc 15.0.3) with the error message explicit type is missing ("int" assumed)

auto foo(){
     const bool retVal = false;
     return retVal;
}

It can be easily be seen, that the return type in this case is bool. Why can auto not be used as a return type for function declaration? This feature would nicely support the DRY principle.


Solution

  • As commented by Jon and Ron, using a compiler fixes this problem.

    In addition, Aki's lambda solution would work:

    auto foo = []() {
      const bool retVal = false;
      return retVal;
    };