Is this really C ++ code? (Using a local type outside the scope of the declaration)

Is the following code valid in accordance with (any) ISO C ++ standards?

#include <functional>

auto a() {
    struct Foo {
    };
    return []() {return Foo{}; };
}

int main()
{
    auto l = a()();
    decltype(l) ll;
    //Foo f; //error: unknown type name 'Foo'
    return 0;
}

Compilers (Visual studio 2015, the last Clang and the last GCC) accept this, but it seems strange that decltype should give me access to Foo,

+4
source share
1 answer

Yes.

This is actually the name of the type covered by the region, not the type itself.

You do not use his name, so everything is in order.

+5
source

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


All Articles