POD和模板 [英] POD and templates

查看:190
本文介绍了POD和模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个POD?

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

如果我有一个C函数需​​要类似:

If i have a C function that requires something like:

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

我可以传递 Data< double> object?

can i pass instead a Data<double> object?

Ps。
我想答案是肯定的,因为在编译时, Data 将被转换为上面的结构,并且将是一个POD结构。
我需要对此进行确认。

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.

推荐答案

在回答第一个问题时,模板参数 T 如果 T 是POD,则数据< T> 将是POD。

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

在回答第二个问题时,具有相同定义的类不是相同类型,因此您不能互换使用它们。 在第一个示例中的数据< double> 将不是与您的第二个定义中的 Data 相同的类型。 (要让他们在同一个程序,你必须给他们不同的名称,你不能有一个类同名的模板)。

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

这篇关于POD和模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆