dimanche 1 novembre 2015

initializer-list constructor causing ambiguity with rvalue constructor

I'm getting an error in Visual Studio C++11 when trying to use initializer-lists as shown in the following code

template <typename T>
struct TestArray
{
  TestArray(std::initializer_list<T> list) {}
  TestArray(TestArray<T> &&rval) {} // This causes an error
};

struct TestPair
{
  TestPair(int a, int b) {}
};

When I declare:

TestArray<TestPair> blah({ { 1, 2 } });

I get the error:

Cannot convert from 'initializer-list' to 'TestArray<TestPair>'
No constructor could take the source type, or constructor overload     resolution was ambiguous

If I remove the rvalue constructor, it works fine. Why is there some kind of ambiguity between the initializer-list and the rvalue constructor here?

Aucun commentaire:

Enregistrer un commentaire