Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POD and templates

Tags:

c++

c

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> object?

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.

like image 662
luke Avatar asked Dec 03 '22 07:12

luke


2 Answers

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.)

like image 89
CB Bailey Avatar answered Dec 18 '22 10:12

CB Bailey


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

If you have access to c++0x or Boost you should be able to check via the trait std::is_pod<mytype>.

Hope this helps.

like image 21
Darren Engwirda Avatar answered Dec 18 '22 09:12

Darren Engwirda