2015-11-02 61 views
0

尝试使用初始化-列表所示下面的代码列表构造引起歧义

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) {} 
}; 

当我得到在Visual Studio C++ 11的错误,当我宣布:

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

我得到的错误:

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

如果我删除了右值的构造函数,它工作正常。为什么在这里的初始化程序列表和右值构造函数之间存在某种模糊性?

+1

卸下外的括号:'等等{{1,2}}'。 –

+0

是否有人可以解释({})语法中断的一般情况?即它似乎在90%的情况下工作,我看到很多人使用它。它被认为是错误的广义规则是什么? – Raj

+0

兼容gcc/clang [Demo](http://coliru.stacked-crooked.com/a/e9e5b517982677c3)。 – Jarod42

回答

0

您的测试是错误的,你不应该有外()的,更像是:

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