Array ctor with nested initializer_list

  • Thread starter Thread starter Artem Pasiukov
  • Start date Start date
A

Artem Pasiukov

Guest
Microsoft Visual Studio Community 2017 Version 15.6.4

Hi!

Question is: why Test t; is compiled while it works not as intended?


template <typename ValueT>
class DValuePair
{
ValueT data_[2] = {};

public:
DValuePair() = default;
DValuePair(const DValuePair&) = default;
DValuePair& operator =(const DValuePair&) = default;
DValuePair(DValuePair&&) = default;
DValuePair& operator =(DValuePair&&) = default;

template <typename... Args>
DValuePair(Args... args)
: data_{forward<Args>(args)...}
{}
};

struct V
{
int i_;
std::string str_;
};

using Pair = DValuePair<V>;

struct Test
{
Pair vv_;

Test()
: vv_{{0, "zero"}, {1, "one"}} // is compiled but vv_ will contain something terrible. Why it is compiled?
//: vv_{0, "zero", 1, "one"} // works fine
{}
};

int main(int argc, char* argv[])
{
V v{2, "two"}; // work fine
Test t; // ???
//Pair vv{{0, "zero"}, {1, "one"}}; // error C2440: 'initializing': cannot convert from 'initializer list' to 'DValuePair<V>'. But it looks same as vv_{{0, "zero"}, {1, "one"}} in Test ctor
Pair vv{0, "zero", 1, "one"}; // works fine

return 0;
}

Continue reading...
 
Back
Top