c++ - Apply a (sort of a meta) function to a sequence of types -
i have function:
template <typename t> std::string foo();
which think of taking type input , producing string.
i have either parameter pack, or tuple, more convenient you; suppose it's
using std::tuple<myparameters...> my_types;
now, want invoke foo<t>
on each type t
in pack, or in tuple's type definition, in sequence.
i realize can using library such boost's mpl or boost hana, don't want stick of code, , wondering if principle of doing "captured" in succinct.
notes:
- bonus points if provide answer works generic lambda's instead of templated function.
- answer must c++14, not c++17.
imho, sort of things need partial specialization. struct/classes, non functions.
so if can demand work method of variadic struct bar
, can write foo()
calling method in bar
(the operator()
, example) follows
template <typename t> std::string foo () { return bar<t>()(); }
the following full working (i don't know if "succinct" enough) example; it's c++11, if i'm not wrong.
#include <tuple> #include <complex> #include <iostream> template <typename ...> struct bar; template <typename t0, typename ... ts> struct bar<t0, ts...> { std::string operator() () { return "type; " + bar<ts...>()(); } }; template <template <typename ...> class c, typename ... ts1, typename ... ts2> struct bar<c<ts1...>, ts2...> { std::string operator() () { return "type container; " + bar<ts1...>()() + bar<ts2...>()(); } }; template <> struct bar<> { std::string operator() () { return {}; } }; template <typename t> std::string foo () { return bar<t>()(); } int main () { std::cout << foo<int>() << std::endl; std::cout << foo<std::tuple<int, long, short, std::tuple<long, int>>>() << std::endl; std::cout << foo<std::complex<double>>() << std::endl; }
p.s.: isn't clear me mean "an answer works generic lambda's instead of templated function".
can show example of use?
Comments
Post a Comment