aga
aga

Reputation: 357

Best way to provide Function argument

I have an Api which takes two struct as an arguments

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

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

Also I have to write another API which has same requirement but instead of int a in bundle struct it should be double.

I can write 2 structs(1 for int and 1 for double) but it seems not good and if I am use the struct , the functions have too many arguments(3 agruments for start and 3 agruments for end). Please suggest some correct way of doing this problem.

Also If I have to use default argument for endBundle, How can I use it?

Upvotes: 4

Views: 113

Answers (1)

TartanLlama
TartanLlama

Reputation: 65770

You could make bundle a template:

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

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

Or you could use std::array:

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

If you want different types in the same bundle, you could use std::tuple:

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

Or make bundle take three template arguments:

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

Upvotes: 8

Related Questions