2011-03-29 9 views
6

मैं एक सरणी बनाना चाहता था:C++ प्रारंभकर्ता सूचियों और variadic टेम्पलेट्स

template < typename T, typename ... A > struct a { 
    T x [1 + sizeof... (A)]; 
    a() = default; 
    a (T && t, A && ... y) : x { t, y... } {} 
}; 

int main() { 
    a < int, int > p { 1, 1 }; // ok 
    a < a < int, int >, a < int, int > > q { { 1, 1 }, { 3, 3 } }; // error: bad array initializer 
} 

यह क्यों संकलन नहीं करता है? (जी ++ 4.6 के साथ परीक्षण किया गया)

+1

इतना जटिल क्यों o0 – orlp

+0

यह क्या त्रुटियां फेंकता है? – ChrisE

+3

मैं * सोचता हूं * यह एक बग है। – GManNickG

उत्तर

2

मुझे यकीन है कि यह एक बग है। तर्क के साथ कन्स्ट्रक्टर की आपूर्ति के लिए () के स्थान पर उपयोग किया जा सकता है। इसलिए आपका कोड ठीक होना चाहिए:

int main() 
{ 
    // this is fine, calls constructor with {1, 1}, {3, 3} 
    a<a<int, int>, a<int, int>> q({ 1, 1 }, { 3, 3 }); 

    // which in turn needs to construct a T from {1, 1}, 
    // which is fine because that's the same as: 
    a<int, int>{1, 1}; // same as: a<int, int>(1, 1); 

    // and that's trivially okay (and tested in your code) 

    // we do likewise with A..., which is the same as above 
    // except with {3, 3}; and the values don't affect it 
} 

तो पूरी चीज ठीक होनी चाहिए।