Best way to provide function argument

I have an Api that takes two structures as arguments

struct bundle{
int a;
int b;
int c;
};

void func(const bundle& startBundle, const bundle& endBundle);

I also need to write another API that has the same requirement, but instead of int a in the struct bundle it should be double.

I can write 2 structures (1 for int and 1 for double), but this seems not very good, and if I use struct, the functions have too many arguments (3 start to start and 3 agruments to end). Please suggest the correct way to solve this problem.

Also, if I need to use the default argument to endBundle, how to use it?

+4
source share
1 answer

You can make a bundletemplate:

template <typename T>
struct bundle {
    T a;
    T b;
    T c;
};

void func(const bundle<int>& startBundle, const bundle<int>& endBundle); 

Or you can use std::array:

using IntBundle = std::array<int, 3>;
using DoubleBundle = std::array<double, 3>;
void func(const IntBundle& startBundle, const IntBundle& endBundle); 

, std::tuple:

using IntBundle = std::tuple<int,int,int>;
using OtherBundle = std::tuple<float,int,int>; 

bundle :

template <typename A, typename B, typename C>
struct bundle {
    A a;
    B b;
    C c;
};
+8

Source: https://habr.com/ru/post/1623727/


All Articles