I can find examples of how to write variadic functions with one type that repeats like StringFormat()
but I need to write one where two different types repeat alternating.
The usage needs to look like this:
MyFunc(stringVar, intVar1, doubleVar1);
MyFunc(stringVar, intVar1, doubleVar1, intVar2, doubleVar2, intVar3, doubleVar3, intVar4, doubleVar4);
where the function always has a string input and a minimum of one int and one double but where the int and double would repeat together.
If possible I also need a way to have the documentation tell you which arg type you're on. Maybe the documentation generates automatically and just works but just in case there's something special you need to do please include that too or if it's not possible to have intellisense work for that please let me know. Something like this maybe: ???
/// <summary>
/// MyFunc Summary
/// </summary>
/// <param name="name">Name description</param>
/// <param name="event1_id">ID for Event 1</param>
/// <param name="event1_value">Value for Event 1</param>
/// ...
/// <param name="eventN_id">ID for Event N</param>
/// <param name="eventN_value">Value for Event N</param>
/// <returns>return description</returns>
If I understand what you're trying to do, you can write the following function:
template<typename ...Ts>
void MyFunc(std::string Var, Ts ...ts)
{
// simple structure to store each pair of arguments
struct Arg { int i; double d; };
// checks narrowing conversions,
// and incorrect type/number of arguments
std::array<Arg, sizeof...(Ts) / 2> arr { ts... };
// ensures at least one int and double argument
static_assert(std::size(arr) > 1);
// ...
}
Here's a demo.