Yesterday, I was (pleasantly) surprised when I was able to compile code that had a method which used a using type alias even though the declaration of the alias was not until later in the class definition.
- Is this 'forward' usage of a type-alias valid? (I assume it is, since Clang 5 and GCC 4.9 both work this way.)
- What rules cover this behavior and the difference between the method body and method signature usages?
Case #1 - using declared after method, valid inside method body (only?)
#include <string>
#include <iostream>
struct X {
std::string create() { // fails to compile if Y used in signature
return Y{"hello!"}; // compiles when Y here
}
using Y = std::string; // declared at bottom
};
int main()
{
std::cout << X().create() << std::endl;
}
Case #2 - using declared above is [also] valid in signature
#include <string>
#include <iostream>
struct X {
using Y = std::string; // declared at top
Y create() { // can use Y here as well
return Y{"hello!"};
}
};
int main()
{
std::cout << X().create() << std::endl;
}