可变参数模板函数解包顺序

Variadic template function unpack order

本文关键字:包顺序 顺序 函数 变参 参数      更新时间:2023-10-16

我有这个代码:

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)...);

这本质上与:

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...>{});
}

不保证函数调用参数的顺序。因此,abc(get<T>(str)...);没有定义的顺序。

有关更多详细信息,请参阅C++函数参数中的计算顺序。