2015-11-02 56 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}};