POD and templates

Is it 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 a Data<double> object?

Ps. I think the answer is yes, since at compile time the Data<dobule> will be translated into the structure above and will be the POD structure. I only need and (informed) confirmation about this.

+4
source share
2 answers

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

In response to your second question, classes with identical definitions are not identical, so you cannot use them interchangeably. Data<double> in the first example will not be the same type as the Data in the second definition. (To have them in the same program, you would have to give them different names, one way or another. You cannot have a template with the same name as the class.)

+14
source

It depends on what type you go through as T If you create an instance of type POD , then yes.

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

Hope this helps.

+3
source

Source: https://habr.com/ru/post/1369289/


All Articles