I was watching one of Jason Turner's videos and I saw that you can define a type inside a function scope and have it available outside of that scope through function return type deduction.
auto f()
{
struct MyStruct
{
int n;
};
return MyStruct{};
}
int main()
{
auto a = f().n;
return a;
}
Why is this allowed? Is there a paragraph in the C++ 14 standard that allows this?
When trying to get the typeid
of MyStruct
with clang in compile explorer I saw in the assembly output the type displayed as f()::MyStruct
, so there is a scope, but somehow I can access MyStruct
outside of that scope. Is this some kind of ADL thing?