Sorry for the inability to explain the primary Q in the title itself due to complexity of the problem.
Need to pass various types of std::pair
s to a method like below:
foo({1, 1} , {2, 2.2}, {3, "3"}); // 'first' is always `int`
However, I couldn't figure out a syntax for How to define foo()
using variadic templates?
This is more of a cosmetic change, where the intention is to avoid the boiler plate code. Hence below suggestion is ruled out:
template<typename... Args>
void foo (Args&&... args) { ... }
template<typename T> using pair = std::pair<int, T>;
foo(pair<int>{1, 1} , pair<double>{2, 2.2}, pair<std::string>{3, "3"});
For anyone who is interested, what I am going to do with various pair
s. An overloaded function will be invoked on all the args...
(using array trick) and all the second
values will be converted to a std::string
. For those who don't know the famous array trick:
const string array[] = { MyFunc(std::forward<Pairs>(pairs)) ... };
Similar (but not duplicate) question: Passing multiple std::pair to variadic template constructor using { }, { }