Home > Back-end >  Call template-function with types in a tuple (not values)
Call template-function with types in a tuple (not values)

Time:02-03

I would like to call a template function with no arguments for each type in a tuple. The code below shows exactly what the intention is.

My solution involves making a dummy instance of DataGroup(). I'd like to avoid this, as the types may not have a default constructor.

I've attempted to use std::declval<DataGroup>() instead, this results in 'std::declval': Symbol involving type with internal linkage not defined (in msvc).

#pragma once
#include <tuple>

template<typename T>
void do_something_based_on_the_type()
{
    // ...
}

template<template <typename...> typename Tuple, typename... Ts>
void do_something_based_on_the_types_in_a_tuple(Tuple<Ts...>)
{
    (do_something_based_on_the_type<Ts>(), ...);
}

void some_code()
{
    struct Dataset1 {};
    struct Dataset2 {};
    struct Dataset3 {};
    using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;

    do_something_based_on_the_types_in_a_tuple(DataGroup()); // -> ugly? requires a dummy instantiation of the tuple
}



My preferred solution, based on two of the answers together:

namespace internal
{

template<template <typename...> typename Tuple, typename TemplateFunc, typename... Ts>
void call_per_type_in_tuple(std::type_identity<Tuple<Ts...>>, TemplateFunc f)
{
    (f.template operator () < Ts > (), ...);
}

}

template<typename Tuple, typename TemplateFunc>
void call_foreach_tuple_type(TemplateFunc f)
{
    internal::call_per_type_in_tuple(std::type_identity<Tuple>(), std::forward<TemplateFunc>(f));
}

void example()
{
    struct Dataset1 {};
    struct Dataset2 {};
    struct Dataset3 {};
    using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;

    call_foreach_tuple_type<DataGroup>([]<typename T>()
    {
        // ...
    });
}

CodePudding user response:

You can pass in std::type_identity which is always default-constructible

#include <type_traits>

template<template <typename...> typename Tuple, typename... Ts>
void do_something_based_on_the_types_in_a_tuple(std::type_identity<Tuple<Ts...>>)
{
    (do_something_based_on_the_type<Ts>(), ...);
}

using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;
do_something_based_on_the_types_in_a_tuple(std::type_identity<DataGroup>());

CodePudding user response:

Variant with class template specialization:

#include <tuple>

template<typename T>
void do_something_based_on_the_type()
{
    // ...
}

template<typename x_Tuple>
class t_ForEachItem;

template<typename... x_Items>
class t_ForEachItem<::std::tuple<x_Items...>>
{
    public: static void
    Do(){ (do_something_based_on_the_type<x_Items>(), ...); }
};

void some_code()
{
    struct Dataset1 {};
    struct Dataset2 {};
    struct Dataset3 {};
    using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;

    t_ForEachItem<DataGroup>::Do();
}

It can also be used with a function call:

template<typename x_Tuple>
void do_something_based_on_the_types_in_a_tuple()
{
   t_ForEachItem<x_Tuple>::Do();
}
...
do_something_based_on_the_types_in_a_tuple<DataGroup>();

Generic variant:

#include <tuple>

template<typename x_Tuple>
class t_ForEachItem;

template<typename... x_Items>
class t_ForEachItem<::std::tuple<x_Items...>>
{
    public: template<typename x_Fun> static void
    Do(x_Fun fun){ (fun.template operator ()<x_Items>(), ...); }
};

void some_code()
{
    struct Dataset1 {};
    struct Dataset2 {};
    struct Dataset3 {};
    using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;

    t_ForEachItem<DataGroup>::Do([]<typename T>(){});
}

online compiler

CodePudding user response:

My solution is to use a helper function for type deduction. This helper function will take a pointer to a DataGroup instance, and use it to deduce the types in the tuple.

template<typename T>
void do_something_based_on_the_type()
{
    // ...
}

template<typename T>
void do_something_based_on_the_types_in_a_tuple_helper(T* dataGroup)
{
    std::apply([](auto&&... args){ (do_something_based_on_the_type<decltype(args)>(), ...); }, *dataGroup);
}

void some_code()
{
    struct Dataset1 {};
    struct Dataset2 {};
    struct Dataset3 {};
    using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;

    DataGroup dataGroup;
    do_something_based_on_the_types_in_a_tuple_helper(&dataGroup);
}
  • Related