Why is ctor with default argument std::initializer_list unavailable (VS2019)?

Viewed 100

I can't quite figure out why this code doesn't compile on Visual Studio 2019 (16.8.3):

#include <initializer_list>

struct Foo
{
    Foo(std::initializer_list<int> = {});
};

int main()
{
    Foo f;
}

Foo::Foo(std::initializer_list<int>) {}

It gives me this error:

C2512: "Foo": no appropriate default constructor available

Is this a compiler bug or am I missing something here? Note that I've checked and this does compile on GCC 10.1

If you change the forward declaration of the constructor to an immediate definition the code compiles without errors:

#include <initializer_list>

struct Foo
{
    Foo(std::initializer_list<int> = {}) {}
};

int main()
{
    Foo f;
}
1 Answers

There is a compiler bug here.

It shouldn't be failing compilation just because the constructor's definition isn't available in the same translation unit (providing it in another one, or even providing it underneath main, doesn't permit the program to build).

If you swap std::initializer_list for int it all works as expected.

I have reported this issue to Microsoft. (Will add link when available)

Related