This is currently in pseudo code as it is an idea that I'm working on before I begin to write it as full code.
I know that I can create a normal variadic function that uses va_arg
and va_list
such as printf()
does, however, I want to avoid using them completely.
I was thinking about using templated variadic parameters instead. I was thinking of making an instantiable class templated using variadic parameters. The condition here is that this class's constructor can only ever accept two types, but the number of either type can vary. I know that it is agnostic to where the parameters are written in code compared to how the compiler will interpret and the order in which they are called, but that's not an issue. The ordering I've chosen for the parameters is by convention for readability and consistency purposes.
Here's an example of the pseudo-code:
class TypeIn {...}
class TypeOut{...}
template<typename... T1, typename... T2>
class MyObject {
std::array<TypeIn*> inputs_;
std::array<TypeOut*> outputs_;
public:
MyObject(T1&&... inputs, T2&& ... outputs) { ... }
};
Since I'm still working in C++17 and don't have C++20 with concepts, modules, and coroutines just yet, what would be the cleanest most reliable and efficient way to make sure that T1
is a TypeIn
and T2
is a TypeOut
class object and to populate the arrays accordingly? I could use vector, but once the object is constructed, the sizes of the inputs and outputs will not change.
Possible use case would be:
using In = TypeIn;
using Out = TypeOut;
MyObject obj( In a, In b, In c, Out x, Out y);
And I'd prefer not to have this for syntax if at all possible:
MyObject<In,In,In,Out,Out> obj( In a, In b, In c, Out X, Out y);
Since the first is cleaner or more readable.
Edit
After doing some thinking I was wondering if this might work instead...
class In {...}
class Out{...}
// template<typename T1 = In, typename T2 = Out>
// T1 must == type In and T2 must == type Out
class MyObject {
private:
std::vector<In*> inputs_;
std::vector<Out*> outputs_;
public:
MyObject() = deafault;
template<typename... Inputs> // would probably use move semantics or forwarding
void assignInputs(Inputs&& ... inputs);
template<typename... Outputs> // would probably use move semantics or forwarding
void assignOutputs(Inputs&& ... outputs);
};
However, this would force the user to have to construct the object, then call both functions... I was trying to do all of this upon construction...