POD y plantillas

is this a POD?

template <class T>
struct Data {
  float val_f; 
  T val_t;    
  int val_i;  
};

If i have a C function that requires something like:

struct Data {
  float val_f; 
  double val_t;    
  int val_i;  
};

can i pass instead a Data<double> objeto?

Ps. I guess the answer is yes, since at compile time the Data<dobule> would be translated to the structure above and would be a POD structure. I need just and (informed) confirmation on this.

preguntado el 27 de agosto de 11 a las 14:08

What do you mean by pass a data object? -

¿Qué te dice tu compilador? -

2 Respuestas

In answer to the first question, it depends on the template parameter T. Data<T> will be POD if T is POD.

In answer to your second question, classes with identical definitions are not identical types so you can't use them interchangeably. Data<double> in the first example would not be the same type as Data in your second definition. (To have them in the same program you would have to give them different names, anyway. You can't have a template with the same name as a class.)

Respondido 27 ago 11, 18:08

Tom: or different namespaces, yes. - CB Bailey

I am interested in the memory layout, since using a cast there would be no problem at compile time. But is Data<double> a POD and equal, as memory layout, to the other Data structure? Is this a portable approach? - Lucas

@luke: Yes Data<double> from the first example and Data from the second example are layout-compatible so must have the same value representation. - CB Bailey

It depends on what type you're passing as T. If you're instantiating with a POD type, then yes.

Si tienes acceso a c++0x or Boost you should be able to check via the trait std::is_pod<mytype>.

Espero que esto ayude.

Respondido 27 ago 11, 18:08

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