c++ - Variadic template function unpack order -
i have code:
template<typename ...t> struct test { void call(string str) { abc(get<t>(str)...); } template<typename u> string get(string& inp) { string ret{ inp[0] }; inp.erase(0, 1); cout << ret << endl; // first "a", next "b", next "c" - ok return ret; } void abc(string a, string b, string c) { cout << << " " << b << " " << c << endl; // "b c a" - why? } };
i'm calling this:
test<int, bool, float> test; test.call("abc");
and output b c a
thought expect a b c
. in get()
function have correct order. why this? can't find rule order.
the order of evaluation of function arguments unspecified.
abc(get<t>(str)...);
that same as:
abc(get<t1>(str), get<t2>(str), get<tn>(str));
you enforce evaluation order generating array store strings, dispatching array:
template <std::size_t n, std::size_t... idx> void call_helper(std::array<std::string, n> arr, std::index_sequence<idx...>) { abc(std::get<idx>(arr)...); } void call(string str) { std::array<std::string,sizeof...(t)> arr { get<t>(str)... }; call_helper(arr, std::index_sequence_for<t...>{}); }
Comments
Post a Comment