Refactoring some code, I found, that if I have member function definition outside of the class, it's return type can't use names defined by "using" inside this class.
#include <iostream>
class Foo {
public:
using Bar = int;
Bar f();
};
Bar Foo::f() {
return 42;
}
int main() {
std::cout << Foo().f();
return 0;
}
This code won't compile, with error: ‘Bar’ does not name a type. I can still use:
Foo::Bar Foo::f() {
return 42;
}
but with long names it looks much worse.
What are the reasons behind this? Why can I simply use Bar as a parameter type, but has to add Foo:: to use it as a return type?
The reason is, where the function is defined, that Bar
is not in scope. It is actually Foo::Bar
, and that is not a candidate for matching an unqualified name Bar
outside the class definition.
So change the definition to
Foo::Bar Foo::f()
{
return 42;
}
If you want to be really clear for the benefit of mere mortals who only have visibility of the header containing the class definition, also change the declaration of the function (within the class definition) in the same way.