在lambda中访问外部作用域名称是g ++或Visual还是两者都没有,对吧?

Che*_*Alf 7 c++ lambda

码:

auto main() -> int
{
    int b = 42;
    auto lambasta = [&]()
    {
        using B_type = decltype( b );
        return B_type{};
    };
    (void) lambasta;
}
Run Code Online (Sandbox Code Playgroud)

使用MinGW g ++ 6.3.0进行无诊断编译-std=c++14 -Wall -pedantic-errors.无法使用Visual C++ 2015 update 3进行编译,

foo.cpp(6): error C2065: 'b': undeclared identifier

Evg*_*niy 1

可能的解决方法:

template<typename T>
struct wrapper
{
    using wrapped_t = T;
};

auto main() -> int
{
    int b = 42;
    auto lambasta = [&, a = wrapper<decltype(b)>()]()
    {
        using B_type = typename decltype( a ) ::wrapped_t;
        return B_type{};
    };
    (void) lambasta;
}
Run Code Online (Sandbox Code Playgroud)

适用于 GCC 6.3 和 MSVC 2015 up 3