Why can't my static auto-function type be selected within the class?

I am trying to get the return type of an auto function. This works :

 auto foo(int bar) { return 0; } typedef std::result_of<decltype(foo)> foo_t; 

Great, here is the next step: getting the return type of the static auto function in the class scope. This also works :

 struct Foo { static auto foo(int bar) { return 0; } }; typedef std::result_of<decltype(Foo::foo)> foo_t; 

But this does not work :

 struct Foo { static auto foo(int bar) { return 0; } typedef std::result_of<decltype(Foo::foo)> foo_t; }; 

GCC says: “Error: using“ static auto Foo :: foo (int) ”before deducing“ auto ”, says Klang:“ The function “foo” with the returned return type cannot be used before it is determined. ”Why?

+5
source share
1 answer

Although the way code is written makes it possible, the definition of the class foo() in a class can only be processed after the class has been fully defined. It is as if you wrote this:

 struct Foo { static auto foo(int bar); typedef std::result_of<decltype(Foo::foo)> foo_t; }; auto Foo::foo(int bar) { return 0; } 

The definition of foo() allows the use of types defined in class Foo , including foo_t , which will be circular. Therefore, the definition of class Foo does not allow the use of the definition of its member functions - only their declarations.

In other words, you assume that the code is completely ranked top to bottom. This is not true.

+9
source

Source: https://habr.com/ru/post/1263295/


All Articles