mar*_*rco 11 c++ containers templates variadic
I have an overloaded function that I have to call with many different types. The simple approach is:
uint8_t a;
uint16_t b;
//....
double x;
doSomething(a);
doSomething(b);
//...
doSomething(x);
Run Code Online (Sandbox Code Playgroud)
expressing those calls succinctly can be done with a variadic template as explained at this Q&A. The code will look somewhat like this:
auto doSomethingForAllTypes = [](auto&&... args) {
(doSomething(args), ...);
};
uint8_t a;
uint16_t b;
//....
double x;
doSomethingForAllTypes(a, b, ... ,x);
Run Code Online (Sandbox Code Playgroud)
But I'll have to do this at many places in the code, so I'd like to define the list of types only once. I'd like to have code that looks conceptually like this:
auto doSomethingForAllTypes = [](auto&&... args) {
(doSomething(args), ...);
};
someContainer allTypesNeeded(uint8_t, uint16_t, ... double);
doSomethingForAllTypes(allTypesNeeded);
Run Code Online (Sandbox Code Playgroud)
How could this be done?
Sto*_*ica 10
With some boiler-plate to obtain a tuple foreach, you can implement what you want as follows:
#include <tuple>
#include <utility>
namespace detail
{
template<typename T, typename F, std::size_t... Is>
void for_each(T&& t, F f, std::index_sequence<Is...>)
{
( static_cast<void>(f(std::get<Is>(std::forward<T>(t)))),... );
}
}
template<typename... Ts, typename F>
void for_each_in_tuple(std::tuple<Ts...> const& t, F f)
{
detail::for_each(t, f, std::index_sequence_for<Ts...>{});
}
int main() {
std::tuple<uint8_t, uint16_t, double> tup{};
for_each_in_tuple(tup, [](auto&& arg) {
doSomething(arg);
});
}
Run Code Online (Sandbox Code Playgroud)
如果要具有预定义的类型序列,并且不想创建带有参数的元组,则可以使用TypeList方法:
#include <type_traits>
#include <utility>
void doSomething(int)
{
}
void doSomething(double)
{
}
template <typename... Args>
void doSomething(Args&&... args)
{
(doSomething(std::forward<Args>(args)), ...);
}
template <typename ...Args>
struct TypeList{};
template <typename T>
struct DoSomethingHelper;
template <typename ...Args>
struct DoSomethingHelper<TypeList<Args...>>
{
static void doSomething(Args&&... args)
{
::doSomething(std::forward<Args>(args)...);
}
};
template <typename T, typename ...Args>
void doSomethingForTypes(Args&&... args)
{
DoSomethingHelper<T>::doSomething(std::forward<Args>(args)...);
}
int main()
{
using MyTypeList = TypeList<int, double, int>;
doSomethingForTypes<MyTypeList>(1, 1.0, 2);
}
Run Code Online (Sandbox Code Playgroud)
与std::tuple和std::apply
std::tuple<uint8_t, uint16_t, double> tup{};
std::apply([](const auto&... arg) { (doSomething(arg), ...); }, tup);
Run Code Online (Sandbox Code Playgroud)