Using the capabilities of C ++ 14, you can get a general solution:
struct Msg { std::vector<int> values; }; template <std::size_t... indices, typename Tuple, typename OutputIt> void copy(std::index_sequence<indices...>, Tuple&& t, OutputIt out) { (void)std::initializer_list<int> { (*out++ = std::get<indices>(std::forward<Tuple>(t)), 0)... }; } template <typename Tuple, typename OutputIt> void copy(Tuple&& t, OutputIt out) { copy(std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>{}>{}, std::forward<Tuple>(t), out); } template <typename... Args> Msg func(Args... args) { auto cat = std::tuple_cat(args...); Msg m{{sizeof...(args)}}; copy(cat, std::back_inserter(m.values)); return m; }
The use is more flexible than before:
Msg msg = func(foo(1,2,3), foo(4), foo(5,6,7));
Demo
You can define foo as a tuple (fixed size), for example. using foo = std::tuple<int, int>; , then your example above compiles without an auxiliary function (after setting curly braces, of course).
source share