为发烧友tuple_for_each

时间:2015-08-03 05:48:47

标签: c++ templates variadic-templates template-meta-programming sfinae

背景/动机

我一直在玩VC ++ 2015,看一些编写实用程序例程来处理元组和其他可变零碎的方法。

我感兴趣的第一个功能是common-or-garden tuple_for_all函数。对于功能f和元组t,请依次调用f(get<0>(t)f(get<1>(t)等。

到目前为止,非常简单。

template<typename Tuple, typename Function, std::size_t... Indices>
constexpr void tuple_for_each_aux(Function&& f, Tuple&& t, std::index_sequence<Indices...>) {
    using swallow = int[];
    static_cast<void>(swallow{ 0, (std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t))), void(), 0)... });
}

template<typename Function, typename Tuple>
constexpr void tuple_for_each(Function&& f, Tuple&& t) {
    return tuple_for_each_aux(std::forward<Function>(f), std::forward<Tuple>(t), std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
}

好的,这是有效的(模块化任何编译和复制/粘贴在减少它时发生的错误)。

但我的下一个想法是Function在某些情况下可能会返回一个有用/有趣的值,因此我们应该捕获它。天真地,我们可以这样做:

template<typename Tuple, typename Function, std::size_t... Indices>
constexpr auto tuple_for_each_aux(Function&& f, Tuple&& t, std::index_sequence<Indices...>) {
    return std::make_tuple(std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t)));
}

template<typename Function, typename Tuple>
constexpr auto tuple_for_each(Function&& f, Tuple&& t) {
    return tuple_for_each_aux(std::forward<Function>(f), std::forward<Tuple>(t), std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
}

这对函数确实返回一个值很有用,但由于void令人讨厌地退化而且我们无法生成std::tuple<void>,因此它不适用于void - 返回功能。我们不能通过返回类型直接重载,但是C ++为我们提供了使用SFINAE处理它的工具:

template<typename Function, typename Tuple, std::size_t... Indices, typename = std::enable_if_t<std::is_void<std::result_of_t<Function(std::tuple_element_t<0, std::decay_t<Tuple>>)>>::value>>
constexpr void tuple_for_each_aux(Function&& f, Tuple&& t, std::index_sequence<Indices...>) {
    using swallow = int[];
    static_cast<void>(swallow{ 0, (std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t))), void(), 0)... });
}

template<typename Function, typename Tuple, std::size_t... Indices, typename = std::enable_if_t<!std::is_void<std::result_of_t<Function(std::tuple_element_t<0, std::decay_t<Tuple>>)>>::value>>
constexpr decltype(auto) tuple_for_each_aux(Function&& f, Tuple&& t, std::index_sequence<Indices...>) {
    return std::make_tuple(std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t)))...);
}

template<typename Function, typename Tuple>
constexpr decltype(auto) tuple_for_each(Function&& f, Tuple&& t) {
    return tuple_for_each_aux(std::forward<Function>(f), std::forward<Tuple>(t), std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
}

这很好。如果评估顺序在两者之间是一致的(void版本是从左到右,值返回版本由编译器决定,那么可能是从右到左)。我们可以通过避开对std::make_tuple的调用来解决这个问题,而是支持初始化std::tuple。我不知道是否有比decltype(std::make_tuple(...))更好的东西来获得正确的类型来构建。可能有。

template<typename Function, typename Tuple, std::size_t... Indices, typename = std::enable_if_t<std::is_void<std::result_of_t<Function(std::tuple_element_t<0, std::decay_t<Tuple>>)>>::value>>
constexpr void tuple_for_each_aux(Function&& f, Tuple&& t, std::index_sequence<Indices...>)
{
    using swallow = int[];
    static_cast<void>(swallow{ 0, (std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t))), void(), 0)... });
}

template<typename Function, typename Tuple, std::size_t... Indices, typename = std::enable_if_t<!std::is_void<std::result_of_t<Function(std::tuple_element_t<0, std::decay_t<Tuple>>)>>::value>>
constexpr decltype(auto) tuple_for_each_aux(Function&& f, Tuple&& t, std::index_sequence<Indices...>)
{
    return decltype(std::make_tuple(std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t)))...)){std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t))) ...};
}

template<typename Tuple, typename Function>
constexpr decltype(auto) tuple_for_each(Function&& f, Tuple&& t)
{
    return tuple_for_each_aux(std::forward<Function>(f), std::forward<Tuple>(t), std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
}

(顺便说一下,VC ++ 2015现在似乎有问题;它still doesn't use left-to-right evaluation即使是支持初始化程序,因为优化程序团队doesn't seem to think it's important

我对std::enable_if_t检查更感兴趣。我们不检查该函数是否为元组中的每个类型返回非void,仅返回第一个。但实际上,它应该是全有或全无。 Columbo's all_true技术为我们解决了这个问题:

template <bool...> struct bool_pack;

template <bool... v>
using all_true = std::is_same<bool_pack<true, v...>, bool_pack<v..., true>>;

template<typename Function, typename Tuple, std::size_t... Indices, typename = std::enable_if_t<all_true<std::is_void<std::result_of_t<Function(std::tuple_element_t<Indices, std::decay_t<Tuple>>)>>::value...>::value>>
constexpr void tuple_for_each_aux(Function&& f, Tuple&& t, std::index_sequence<Indices...>)
{
    using swallow = int[];
    static_cast<void>(swallow{ 0, (std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t))), void(), 0)... });
}

template<typename Function, typename Tuple, std::size_t... Indices, typename = std::enable_if_t<!std::is_void<std::result_of_t<Function(std::tuple_element_t<0, std::decay_t<Tuple>>)>>::value>>
constexpr decltype(auto) tuple_for_each_aux(Function&& f, Tuple&& t, std::index_sequence<Indices...>)
{
    return decltype(std::make_tuple(std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t)))...)){std::forward<Function>(f)(std::get<Indices>(std::forward<Tuple>(t))) ...};
}

template<typename Function, typename Tuple>
constexpr decltype(auto) tuple_for_each(Function&& f, Tuple&& t)
{
    return tuple_for_each_aux(std::forward<Function>(f), std::forward<Tuple>(t), std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
}

问题

但这里有点棘手。虽然tuple_for_each是好的和有用的,但我想,如果我把它加了一点怎么办?如果tuple_for_each采用函数f和元组t0t1等,并计算f(get<0>(t0), get<0>(t1)...)f(get<1>(t0), get<1>(t1)...)等等?< / p>

天真的我们想做这样的事情:

    using swallow = int[];
    static_cast<void>(swallow{ 0, ((std::forward<Function>(f)(std::get<Indices>(std::forward<Tuples>(ts))...)), void(), 0)... });

天真地,我们希望第一个...展开Tuples,第二个...展开Indices。但参数包扩展不提供这种控制。如果...之前的表达式包含多个参数包,那么...会尝试并行解压所有这些参数包(VC ++;它会发出编译器错误,它们的长度不同),或者找不到参数完全打包(g ++;它会发出编译器错误,没有包)。

幸运的是,这种情况可以通过额外的间接层来处理,以分离出扩展:

template <bool...> struct bool_pack;

template <bool... v>
using all_true = std::is_same<bool_pack<true, v...>, bool_pack<v..., true>>;

template<size_t N, typename Function, typename... Tuples, typename = std::enable_if_t<std::is_void<std::result_of_t<Function(std::tuple_element_t<N, std::decay_t<Tuples>>...)>>::value>>
constexpr void tuple_for_each_aux(Function&& f, Tuples&&... ts)
{
    return std::forward<Function>(f)(std::get<N>(std::forward<Tuples>(ts))...);
}

template<typename Function, typename... Tuples, std::size_t... Indices, typename = std::enable_if_t<all_true<std::is_void<std::result_of_t<Function(std::tuple_element_t<0, std::decay_t<Tuples>>...)>>::value>::value>>
constexpr void tuple_for_each_aux(Function&& f, std::index_sequence<Indices...>, Tuples&&... ts)
{
    using swallow = int[];
    static_cast<void>(swallow{ 0, (tuple_for_each_aux<Indices>(std::forward<Function>(f), std::forward<Tuples>(ts)...), void(), 0)... });
}

template<std::size_t N, typename Function, typename... Tuples, typename = std::enable_if_t<!std::is_void<std::result_of_t<Function(std::tuple_element_t<N, std::decay_t<Tuples>>...)>>::value>>
constexpr decltype(auto) tuple_for_each_aux(Function&& f, Tuples&&... ts)
{
    return std::forward<Function>(f)(std::get<N>(std::forward<Tuples>(ts))...);
}

template<typename Function, typename... Tuples, std::size_t... Indices, typename = std::enable_if_t<all_true<!std::is_void<std::result_of_t<Function(std::tuple_element_t<0, std::decay_t<Tuples>>...)>>::value>::value>>
constexpr decltype(auto) tuple_for_each_aux(Function&& f, std::index_sequence<Indices...>, Tuples&&... ts)
{
    return decltype(std::make_tuple(tuple_for_each_aux<Indices>(std::forward<Function>(f), std::forward<Tuples>(ts)...)...)) { tuple_for_each_aux<Indices>(std::forward<Function>(f), std::forward<Tuples>(ts)...)... };
}

template<typename Function, typename Tuple, typename... Tuples>
constexpr decltype(auto) tuple_for_each(Function&& f, Tuple&& t, Tuples&&... ts)
{
    return tuple_for_each_aux(std::forward<Function>(f), std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{}, std::forward<Tuple>(t), std::forward<Tuples>(ts)...);
}

这很好......除了......那些讨厌的enable_ifs。我不得不削弱它们,回到仅测试元组中的第一个元素。现在,这不是一场彻底的灾难,因为最内层的扩展可以执行检查。但它并不好。请考虑以下事项:

struct functor
{
    int operator()(int a, int b) { return a + b; }
    double operator()(double a, double b) { return a + b; }
    void operator()(char, char) { return;  }
};

int main()
{
    auto t1 = std::make_tuple(1, 2.0, 'a');
    auto t2 = std::make_tuple(2, 4.0, 'b');
    tuple_for_each(functor{}, t1, t2);
    return 0;
}

functor对象需要强制使用void路径,因为对第三个元组元素的求值函数返回void。但我们的启用检查仅查看第一个元素。由于在 SFINAE驱动的“过载”分辨率之后发生故障,SFINAE无法将我们保存在此处。

但同样地,我们不能双重解包enable_if_t表达式,原因与我们在调用函数时无法做到这一点:参数包扩展混淆并试图在同一时间迭代时间。

这就是我脱离困境的地方。我需要一个与用于调用函数的方向相同的间接方向,但我不能立即看到如何编写该间接方式以使其实际工作。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

类型持有人:

template<class...> class typelist {};

别名模板,用于计算将F应用于I中每个元组的Tuples元素的结果:

template<class F, std::size_t I, class...Tuples>
using apply_result_type = decltype(std::declval<F>()(std::get<I>(std::declval<Tuples>())...));

现在计算结果类型列表:

template<class F, std::size_t...Is, class... Tuples>
typelist<apply_result_type<F, Is, Tuples...>...> 
        compute_result_types(typelist<F, Tuples...>, std::index_sequence<Is...>);

template<class F, std::size_t Size, class... Tuples>
using result_types = decltype(compute_result_types(typelist<F, Tuples...>(),
                                                   std::make_index_sequence<Size>()));

并检查其中的类型列表中没有void

template<class... Ts>
all_true<!std::is_void<Ts>::value...> do_is_none_void(typelist<Ts...>);

template<class TL>
using has_no_void_in_list = decltype(do_is_none_void(TL()));

最后是实际的SFINAE(只显示一个):

template<typename Function, typename... Tuples, std::size_t... Indices,
         typename = std::enable_if_t<!has_no_void_in_list<result_types<Function,
                                                                       sizeof...(Indices),
                                                                       Tuples...>>{}>>
constexpr void tuple_for_each_aux(Function&& f, std::index_sequence<Indices...>, 
                                  Tuples&&... ts)
{
    using swallow = int[];
    static_cast<void>(swallow{ 0, (tuple_for_each_aux<Indices>(std::forward<Function>(f), std::forward<Tuples>(ts)...), void(), 0)... });
}

Demo