Aggregate initialization with dependent member initializer, instanciated in lambda

Viewed 61

The title is almost as long as the minimal code producing the error on Visual Studio (tested on both 2017 and 2019):

#include <string>

struct Foo
{   
    std::string a;
    std::string b{"prefix_" + a};
};


int main()
{
    std::string str{"some string"};
    Foo i{str};

    [str]()
    {
        Foo j{str};
    };
}

On GCC and Clang, both instantiation of Foo are okay (local to main, and local to lambda) On Visual Studio, the instantiation in lambda leads to the error:

error C2326: 'auto main::<lambda_a25bbcbd3e2dc2b2209cfbdd50a2113b>::operator ()(void) const': function cannot access 'j'

If Foo::b has a member initializer that does not depend on another member of Foo, there is no error. I suspect this is why the error mention accessing j (reading j.a to initialize j.b).

Is this behaviour standard conformant, or is this a bug in Visual Studio frontend?

PS: The Godbolt example https://godbolt.org/z/68zbYx

0 Answers
Related