如何反转可变参数模板函数的参数顺序?

tow*_*owi 32 c++ templates variadic-functions variadic-templates c++11

我有一个带有varargs模板参数模板函数,就像这样

template<typename Args...>
void ascendingPrint(Args... args) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

我想写

template<typename Args...>
void descendingPrint(Args... args) {
  /* implementation using ascendingPrint()? */
}
Run Code Online (Sandbox Code Playgroud)

如何在传递参数包之前反转参数包 的顺序args,即在伪代码中:

template<typename Args...>
void descendingPrint(Args... args) {
  ascendingPrint( reverse(args) );
}
Run Code Online (Sandbox Code Playgroud)

And*_*owl 25

总体方法和用法


在全部测试方法包括在包装参数为std::tuple引用,利用了完美转发的机器std::forward_as_tuple().

这意味着,在运行时,您应该承担非常小的开销,并且不会进行不必要的复制/移动操作.此外,框架不使用递归(除了编译时递归,这对于生成索引是不可避免的),因此即使在编译器无法管理内联递归函数调用的情况下也不存在运行时开销的风险(这不太可能无论如何,这更像是一个学术论点.

此外,该解决方案是通用的,因为你可以使用它作为一个只有标题-库相反的论点,并以最小的努力来调用你的函数:descending_print()应该只是一个最小的瘦包装周围ascending_print().

它应该是这样的:

MAKE_REVERT_CALLABLE(ascending_print)

template<typename... Args>
void descending_print(Args&&... args)
{
    revert_call(REVERT_ADAPTER(ascending_print), std::forward<Args>(args)...);
} 
Run Code Online (Sandbox Code Playgroud)

以下是对实施的介绍.


第一步:恢复类型序列


这是一种恢复类型序列的简单方法:

#include <tuple>
#include <type_traits>

template<typename, typename>
struct append_to_type_seq { };

template<typename T, typename... Ts>
struct append_to_type_seq<T, std::tuple<Ts...>>
{
    using type = std::tuple<Ts..., T>;
};

template<typename... Ts>
struct revert_type_seq
{
    using type = std::tuple<>;
};

template<typename T, typename... Ts>
struct revert_type_seq<T, Ts...>
{
    using type = typename append_to_type_seq<
        T,
        typename revert_type_seq<Ts...>::type
        >::type;
};
Run Code Online (Sandbox Code Playgroud)

一个小测试程序:

int main()
{
    static_assert(
        std::is_same<
            revert_type_seq<char, int, bool>::type,
            std::tuple<bool, int, char>
            >::value,
        "Error"
        );
}
Run Code Online (Sandbox Code Playgroud)

一个现实的例子.


第二步:恢复元组


下一步是恢复元组.鉴于通常的指数技巧机制:

template <int... Is>
struct index_list { };

namespace detail
{
    template <int MIN, int N, int... Is>
    struct range_builder;

    template <int MIN, int... Is>
    struct range_builder<MIN, MIN, Is...>
    {
        typedef index_list<Is...> type;
    };

    template <int MIN, int N, int... Is>
    struct range_builder : public range_builder<MIN, N - 1, N - 1, Is...>
    { };
}

template<int MIN, int MAX>
using index_range = typename detail::range_builder<MIN, MAX>::type;
Run Code Online (Sandbox Code Playgroud)

与上面定义的函数一起,可以通过这种方式轻松恢复元组:

template<typename... Args, int... Is>
typename revert_type_seq<Args...>::type
revert_tuple(std::tuple<Args...> t, index_list<Is...>)
{
    using reverted_tuple = typename revert_type_seq<Args...>::type;

    // Forwarding machinery that handles both lvalues and rvalues...
    auto rt = std::forward_as_tuple(
            std::forward<
                typename std::conditional<
                    std::is_lvalue_reference<
                        typename std::tuple_element<Is, reverted_tuple>::type
                        >::value,
                    typename std::tuple_element<Is, reverted_tuple>::type,
                    typename std::remove_reference<
                        typename std::tuple_element<Is, reverted_tuple>::type
                        >::type
                    >::type
                >(std::get<sizeof...(Args) - Is - 1>(t))...
        );

    return rt;
}

template<typename... Args>
typename revert_type_seq<Args...>::type
revert_tuple(std::tuple<Args...> t)
{
    return revert_tuple(t, index_range<0, sizeof...(Args)>());
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的测试程序:

#include <iostream>

int main()
{
    std::tuple<int, int, char> t(42, 1729, 'c');
    auto rt = revert_tuple(t);

    std::cout << std::get<0>(rt) << " "; // Prints c
    std::cout << std::get<1>(rt) << " "; // Prints 1729
    std::cout << std::get<2>(rt) << " "; // Prints 42
}
Run Code Online (Sandbox Code Playgroud)

这是一个实例.


第三步:恢复函数的参数


最后一步是在调用目标函数时解压缩元组.这是另一个通用实用程序,为我们节省了几行:

template<typename... Args>
typename revert_type_seq<Args...>::type
make_revert(Args&&... args)
{
    auto t = std::forward_as_tuple(std::forward<Args>(args)...);
    return revert_tuple(t);
}
Run Code Online (Sandbox Code Playgroud)

上面的函数创建一个元组,其元素是提供的参数,但顺序相反.我们还没准备好定义我们的目标:

template<typename T>
void ascending_print(T&& t)
{
    std::cout << std::forward<T>(t) << " ";
}

template<typename T, typename... Args>
void ascending_print(T&& t, Args&&... args)
{
    ascending_print(std::forward<T>(t));
    ascending_print(std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)

上述函数打印所有提供的参数.以下是我们的写作方式descending_print():

template<typename T, int... Is>
void call_ascending_print(T&& t, index_list<Is...>)
{
    ascending_print(std::get<Is>(std::forward<T>(t))...);
}

template<typename... Args>
void descending_print(Args&&... args) {
    call_ascending_print(make_revert(std::forward<Args>(args)...),
         index_range<0, sizeof...(Args)>());
}
Run Code Online (Sandbox Code Playgroud)

一个简单的测试案例:

int main()
{
    ascending_print(42, 3.14, "Hello, World!");
    std::cout << std::endl;
    descending_print(42, 3.14, "Hello, World!");
}
Run Code Online (Sandbox Code Playgroud)

当然还有一个现实的例子.


最后一步:简化


上述解决方案可能不易理解,但可以使用起来很简单,而且非常灵活.给出了几个通用函数:

template<typename F, typename... Args, int... Is>
void revert_call(F&& f, index_list<Is...>, Args&&... args)
{
    auto rt = make_revert(std::forward<Args>(args)...);
    f(std::get<Is>(rt)...);
}

template<typename F, typename... Args>
void revert_call(F&& f, Args&&... args)
{
    revert_call(f, index_range<0, sizeof...(Args)>(), 
                std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)

还有一些宏定义(我找不到为函数模板创建重载集的方法,抱歉):

#define MAKE_REVERT_CALLABLE(func) \
    struct revert_caller_ ## func \
    { \
        template<typename... Args> void operator () (Args&&... args) \
        { func(std::forward<Args>(args)...); } \
    };

#define REVERT_ADAPTER(func) \
    revert_caller_ ## func()
Run Code Online (Sandbox Code Playgroud)

使用相反的顺序调用任何函数变得非常容易:

MAKE_REVERT_CALLABLE(ascending_print)

template<typename... Args>
void descending_print(Args&&... args)
{
    revert_call(REVERT_ADAPTER(ascending_print), std::forward<Args>(args)...);
}

int main()
{
    ascending_print(42, 3.14, "Hello, World!");
    std::cout << std::endl;
    descending_print(42, 3.14, "Hello, World!");
}
Run Code Online (Sandbox Code Playgroud)

像往常一样,总结一个现实的例子.


mas*_*oud 18

我认为你可以扭转逻辑,而不是颠倒论点!例如,反转参数的操作.

template <typename T>
void ascendingPrint(const T& x)
{
    cout << x << " ";
}

template<typename T, typename ... Args>
void ascendingPrint(const T& t, Args... args)
{
    ascendingPrint(t);                   // First print `t`
    ascendingPrint(args...);             // Then print others `args...`
}

template <typename T>
void descendingPrint(const T& x)
{
    cout << x << " ";
}

template<typename T, typename ... Args>
void descendingPrint(const T& t, Args... args)
{
    descendingPrint(args...);            // First print others `args...`
    descendingPrint(t);                  // Then print `t`
}

int main()
{
    ascendingPrint(1, 2, 3, 4);
    cout << endl;
    descendingPrint(1, 2, 3, 4);
}
Run Code Online (Sandbox Code Playgroud)

产量

1 2 3 4 
4 3 2 1 
Run Code Online (Sandbox Code Playgroud)

  • +1提醒大家,这种概括并不总是最佳选择:-) (3认同)

Ale*_*lex 12

这是一个专门的递归实现revert<>:

// forward decl
template<class ...Tn>
struct revert;

// recursion anchor
template<>
struct revert<>
{
    template<class ...Un>
    static void apply(Un const&... un)
    {
        ascendingPrint(un...);
    }
};

// recursion
template<class T, class ...Tn>
struct revert<T, Tn...> 
{
    template<class ...Un>
    static void apply(T const& t, Tn const&... tn, Un const&... un)
    {
        // bubble 1st parameter backwards
        revert<Tn...>::apply(tn..., t, un...);
    }
};

// using recursive function
template<class A, class ...An>
void descendingPrint(A const& a, An const&... an)
{
    revert<An...>::apply(an..., a);
}
Run Code Online (Sandbox Code Playgroud)

它适用于gcc-4.6/7/8clang,可能符合标准 - 唯一困难的部分是调用revert<Tn...>::apply(tn..., t, un...).

它有缺点(如递归经常),它会生成很多目标函数的模板实例化(代码膨胀)并且不使用完美转发,这可能是一个问题(但可能会改进使用它) .

  • @towi谢谢!我认为这是std C++,但gcc 4.8是我测试过的唯一编译器.[这里](http://liveworkspace.org/code/2Sxk4k$0)是略有修改的版本.适用于gcc 4.6/7/8和clang,但不适用于英特尔编译器. (2认同)
  • 我接受了你的回答,支持非递归的,因为我自己喜欢递归,这个版本对我来说最容易理解.但这是一个艰难的选择:我敦促读者也**阅读其他答案**,[AndyPowl](http://stackoverflow.com/a/15904742/472245)和[Xeo](http://stackoverflow.com/a/15908420/472245),(也许是评论) - 他们可能会有你需要的好处.如果您不需要通用解决方案,[MM](http://stackoverflow.com/a/15904836/472245)也有一点意义. (2认同)

Xeo*_*Xeo 11

这是我在评论中提到的简单方法:反向生成索引并使用它解压缩元组.

// reversed indices...
template<unsigned... Is> struct seq{ using type = seq; };

template<unsigned I, unsigned... Is>
struct rgen_seq : rgen_seq<I-1, Is..., I-1>{};

template<unsigned... Is>
struct rgen_seq<0, Is...> : seq<Is...>{};

#include <tuple>

namespace aux{
template<class Tup, unsigned... Is>
void descending_print(Tup&& t, seq<Is...>)
{
    ascending_print(std::get<Is>(std::forward<Tup>(t))...);
}
} // aux::

template<class... Args>
void descending_print(Args&&... args)
{
    auto t = std::forward_as_tuple(std::forward<Args>(args)...);
    aux::descending_print(t, rgen_seq<sizeof...(Args)>{});
}
Run Code Online (Sandbox Code Playgroud)

实例.


use*_*131 5

这可以使用 C++17 折叠表达式和从右到左顺序执行的小技巧来完成。

#include <iostream>

template< typename T> void print(T&& val) { std::cout << val; }

template< typename ... Types > void descendingPrint(Types&&... vals) {
    int tmps = 0;
    ((print(vals), tmps) = ...);
}

int main() {
    descendingPrint(1, ' ', 2, ' ', 3);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)