Variadic 模板函数解压顺序
Variadic template function unpack order
我有这个代码:
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" - everything is ok
return ret;
}
void abc(string a, string b, string c)
{
cout << a << " " << b << " " << c << endl; // "b c a" - why?
}
};
我是这样称呼它的:
Test<int, bool, float> test;
test.call("abc");
输出是 b c a
,我认为 a b c
。此外,在 get()
函数中我有一个正确的顺序。为什么是这样?我找不到关于此订单的任何规则。
不保证函数调用参数的顺序。因此,abc(get<T>(str)...);
没有定义的顺序。
有关详细信息,请参阅 Order of evaluation in C++ function parameters。
未指定函数参数的计算顺序。
abc(get<T>(str)...);
这与以下基本相同:
abc(get<T1>(str), get<T2>(str), get<TN>(str));
您可以通过生成一个数组来存储字符串,然后从该数组进行调度来强制执行求值顺序:
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...>{});
}
我有这个代码:
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" - everything is ok
return ret;
}
void abc(string a, string b, string c)
{
cout << a << " " << b << " " << c << endl; // "b c a" - why?
}
};
我是这样称呼它的:
Test<int, bool, float> test;
test.call("abc");
输出是 b c a
,我认为 a b c
。此外,在 get()
函数中我有一个正确的顺序。为什么是这样?我找不到关于此订单的任何规则。
不保证函数调用参数的顺序。因此,abc(get<T>(str)...);
没有定义的顺序。
有关详细信息,请参阅 Order of evaluation in C++ function parameters。
未指定函数参数的计算顺序。
abc(get<T>(str)...);
这与以下基本相同:
abc(get<T1>(str), get<T2>(str), get<TN>(str));
您可以通过生成一个数组来存储字符串,然后从该数组进行调度来强制执行求值顺序:
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...>{});
}