开发者

POD and templates

开发者 https://www.devze.com 2023-03-31 08:49 出处:网络
is this a POD? template <class T> struct Data { float开发者_如何学Python val_f; T val_t; int val_i;

is this a POD?

template <class T>
struct Data {
  float开发者_如何学Python 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消