2011-03-29 16 views
6

quería crear una matriz:C++ listas de inicializador y plantillas 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 
} 

¿por qué no compilar? (probado con g ++ 4.6)

+1

Por qué tan complejo o0 – orlp

+0

¿Qué errores arroja? – ChrisE

+3

I * think * that's a bug. – GManNickG

Respuesta

2

Estoy bastante seguro de que es un error. {} se puede usar en lugar de () para proporcionar argumentos al constructor. Por lo tanto, su código debería estar bien:

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 
} 

De modo que todo debería estar bien.

Cuestiones relacionadas