C++ 提供函数参数的最佳方法
我有一个Api,它以两个struct作为参数C++ 提供函数参数的最佳方法,c++,C++,我有一个Api,它以两个struct作为参数 struct bundle{ int a; int b; int c; }; void func(const bundle& startBundle, const bundle& endBundle); 此外,我还必须编写另一个API,该API具有相同的要求,但它应该是双精度的,而不是bundle结构中的int 我可以写2个结构(1个表示int,1个表示double),但似乎不太好,如果我使用这个结构,函数的参数太多(3个表示st
struct bundle{
int a;
int b;
int c;
};
void func(const bundle& startBundle, const bundle& endBundle);
此外,我还必须编写另一个API,该API具有相同的要求,但它应该是双精度的,而不是bundle结构中的int
我可以写2个结构(1个表示int,1个表示double),但似乎不太好,如果我使用这个结构,函数的参数太多(3个表示start,3个表示end)。
请提出解决这个问题的正确方法
另外,如果我必须使用endBundle的默认参数,我如何使用它?您可以将
bundle
作为模板:
template <typename T>
struct bundle {
T a;
T b;
T c;
};
void func(const bundle<int>& startBundle, const bundle<int>& endBundle);
如果希望在同一个捆绑包中使用不同的类型,可以使用std::tuple
:
using IntBundle = std::tuple<int,int,int>;
using OtherBundle = std::tuple<float,int,int>;
在std::array的情况下,所有3个参数都将是双精度的,但我只需要第1个参数就可以是双精度的。@anujgupta啊,我没有捕捉到这一点。请参阅我的编辑。
using IntBundle = std::tuple<int,int,int>;
using OtherBundle = std::tuple<float,int,int>;
template <typename A, typename B, typename C>
struct bundle {
A a;
B b;
C c;
};