我有一个Api,它将两个struct作为参数
struct bundle{
int a;
int b;
int c;
};
void func(const bundle& startBundle, const bundle& endBundle);
Run Code Online (Sandbox Code Playgroud)
此外,我必须编写另一个具有相同要求的API,而不是在bundle结构中的int,它应该是double.
我可以编写2个结构(1个用于int,1个用于double)但似乎不太好,如果我使用结构,则函数有太多的参数(3个开头的agruments和3个结尾的agruments).请提出一些正确的方法来解决这个问题.
另外如果我必须使用endBundle的默认参数,我该如何使用它?
你可以制作bundle一个模板:
template <typename T>
struct bundle {
T a;
T b;
T c;
};
void func(const bundle<int>& startBundle, const bundle<int>& endBundle);
Run Code Online (Sandbox Code Playgroud)
或者您可以使用std::array:
using IntBundle = std::array<int, 3>;
using DoubleBundle = std::array<double, 3>;
void func(const IntBundle& startBundle, const IntBundle& endBundle);
Run Code Online (Sandbox Code Playgroud)
如果您想在同一个包中使用不同的类型,则可以使用std::tuple:
using IntBundle = std::tuple<int,int,int>;
using OtherBundle = std::tuple<float,int,int>;
Run Code Online (Sandbox Code Playgroud)
或者制作bundle三个模板参数:
template <typename A, typename B, typename C>
struct bundle {
A a;
B b;
C c;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |