gpt4 book ai didi

c++ - typedef 如何与模板一起使用?

转载 作者:搜寻专家 更新时间:2023-10-31 00:27:25 25 4
gpt4 key购买 nike

我正在查看项目中的以下代码,但我似乎无法弄清楚数据是如何存储在下面的 typedef 中的。我是 C++ 的新手,所以对 typedef 和模板的了解有限。我搜索了对此类场景的良好描述,但收效甚微。

我的想法是,由 squareTemplate 生成的任何 typedef 都将具有 3 个值:面积、高度和宽度。但我不确定是什么 <bool, bool><std::vector<std ::pair<double, double>>,std::vector<std::pair<int, int>>>做?是否所有 3 个 typedef 都包含面积、高度和宽度变量?请解释。

template <Class D, Class I>
struct squareTemplate
{
I area;
D height;
D width;

squareTemplate() :
area(),
height(),
width()
{
}
};
typedef squareTemplate <std::vector<std ::pair<double, double>>, std::vector<std::pair<int, int>>> squareRange;

typedef squareTemplate <bool, bool> squareValid;

typedef squareTemplate<double, int> squareValue;

最佳答案

当你typedef A B你只是在说 B 是 A 的另一个名字。

Do all 3 typedefs contain area, height, and width variables?

是的。 squareTemplate模板类被定义为具有 area , height , 和 width并且它的所有实例都将具有这些成员。对于您的 typedef:

typedef squareTemplate <std::vector<std ::pair<double, double>>, std::vector<std::pair<int, int>>> squareRange;

area具有第一个模板参数假定的类型 D , 所以 std::vector<std ::pair<double, double>> ; height也是如此和 width - 它们具有第二个模板参数的类型,std::vector<std::pair<int, int>>

按照同样的推理,你得到:

typedef squareTemplate <bool, bool> squareValid;

都是bool

typedef squareTemplate<double, int> squareValue;

areaint ; heightwidthdouble

关于c++ - typedef 如何与模板一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49142838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com