Firma Typedef de C ++ [duplicado]

Posible duplicado:
¿Dónde y por qué tengo que poner las palabras clave "plantilla" y "nombre de tipo"?

Estaba mirando a través this boost::multi_array example y tiene un typedef como:

template <typename Array>
...
typedef typename Array::template subarray<2>::type::iterator iterator2;

Entiendo lo que el

typedef typename <type> <new_type>;

signature looks like, but this has three things after the typedef typename and I can't find online what that would be called or what it does. Can somebody break down what that typedef does and why it has three things? I don't think it's boost-specific.

preguntado el 30 de enero de 12 a las 19:01

4 Respuestas

El template in Array::template works exactly like the typename you already understand. Its role in the typedef is to tell (promise) the compiler that subarray is in fact a template and thus the <2> makes sense because of it.

This is exactly the same as the role of the typename keyword in that typedef, but with the effect of telling the compiler it's a template instead of a type.

Respondido el 30 de enero de 12 a las 23:01

It is the old

typedef typename <type> <new_type>;

you already understand.

type is typename Array::template subarray<2>::type::iterator

new_type is iterator2

Respondido el 30 de enero de 12 a las 23:01

Whenever you write a template function/class, the compiler sometimes needs some help. In this example, Array::template is a type inside of templated type, which makes it require the typename.

Without this need, that typedef would just be:

typedef Array::template subarray<2>::type::iterator iterator2;

However, this will (usually generate) a compiler error. To fix this error, simply insert the typename palabra clave.

typedef typename Array::template subarray<2>::type::iterator iterator2;

Respondido el 30 de enero de 12 a las 23:01

El typename is a helper in this case to let the compiler know this is a type. The keyword is used in templates in instances where the compiler would have trouble choosing between a dependent type and a qualified type.

Respondido el 30 de enero de 12 a las 23:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.