I compile this code below with GCC 11.1.0 with a flag -std=c++17
. It occurs that on the stdout is printed initializer_list
.
I compiled the same code with MSVC with the flag -std=c++17
but it printed "copy constructor". Which compiler is more compliant with the cpp standard? Is compiler free to choose one of the constructors?
#include <iostream>
using namespace std;
struct S
{
S(int) { }
S(initializer_list<int>) { cout << "initializer_list"; }
S(const S&) { cout << "copy constructor"; }
operator int() const { return 1; };
};
int main()
{
S s1(20);
S s2{ s1 };
}
So in this case, VC++ is wrong.
Is VC++ ever right in these language-lawyer debates? – Shoulder