Search code examples
c++c++11auto

Why this inline method with deduced return type not defined yet?


#include <string>
#include <type_traits>

class C
{
    static auto func() { return std::string("hello"); }
    static_assert(std::is_same<decltype(func()), std::string>::value, "");
};

Neither GCC nor Clang accept this, saying that func is used before it is defined. Why?

Changing the deduced auto return type to std::string makes it work.


Solution

  • The decltype construction produces the declared type of an identifier or expression. When func is declared with a return type, then the type of the call expression func() is known and everything works as expected.

    However, when func is declared with the return type placeholder auto, then the declaration of func depends on its definition, so the type of func, and hence of the expression func(), is not known until the function has been defined.

    When you define a class member function inline in the class definition, this is as if the definition were to appear right after the end of the class definition (that is, the function bodies may make reference to names that are lexically declared later in the class definition). The consequence of this and the semantics of auto is that your function auto func is not actually fully declared until the end of the class definition, and thus the type of func() cannot be known until then.