I always thought that when I use initializer list C++ syntax like:
something({ ... });
it's always clear to the compiler that I want to call the overload taking an std::initializer_list
, but it seems this is not so clear for MSVC 2015.
I tested this simple code:
#include <cstdio>
#include <initializer_list>
namespace testing {
template<typename T>
struct Test {
Test() {
printf("Test::Test()\n");
}
explicit Test(size_t count) {
printf("Test::Test(int)\n");
}
Test(std::initializer_list<T> init) {
printf("Test::Test(std::initializer_list)\n");
}
T* member;
};
struct IntSimilar {
int val;
IntSimilar() : val(0) {}
IntSimilar(int v) : val(v) {}
operator int() {
return val;
}
};
}
int main() {
testing::Test<testing::IntSimilar> obj({ 10 });
return 0;
}
and in GCC 6.3 it works as expected, calling Test::Test(std::initializer_list)
but in MSVC 2015 this code calls Test::Test(int)
.
It seems MSVC can somehow ignore the {}
and choose an invalid/unexpected overload to call.
What does the Standard say about this situation? Which version is valid?
Can anybody test this and confirm whether or not this issue remains in MSVC 2017?