0

我有一个实用程序函数,它遍历一个元组,并且对于每个元素,用该元素调用一个函数,最后用所有元组元素的结果调用另一个函数。

为了更好地说明:

  • 有各种类型的元组tuple<Foo<int>, Bar<double>, Baz<char>>
  • 每种类型都有一个隐式接口Foo,该接口返回对某个内部成员的引用。BarBazClassT::data()T&
  • 你有一个带有签名的函数void (int, double, char)

该实用程序遍历元组成员,提取对内部成员的引用,并使用所需参数调用函数。

问题:完美转发

我尝试使用所谓的“通用引用”和完美转发来实现该实用程序,从而消除了对多个左值/右值和 const/non-const 重载的需要。

虽然函数的参数是类型

template<typename Tuple>
auto invoke(Tuple&& tuple)

我无法将左值绑定到它。为什么是这样?

我在这里提出了一个类似的问题,这个问题已经解决了;但是,由于这是一个不相关的问题,我相信这需要一个新问题

ideone 示例:https ://ideone.com/lO5JOB

#include <tuple>
#include <iostream>

// sequence

template<size_t...>
struct Sequence
{ };

template<size_t N, size_t... Seq>
struct GenerateSequence : GenerateSequence<N - 1, N - 1, Seq...>
{ };

template<size_t... Seq>
struct GenerateSequence<0, Seq...>
{
    using type = Sequence<Seq...>;
};

// invoke tuple

struct TupleForEachInvoker
{
    template<typename Func, typename ForEachFunc, typename Tuple, size_t... Seq>
    static auto invoke(Func&& func, ForEachFunc&& forEachFunc, Tuple&& tuple, Sequence<Seq...>)
        -> decltype(func(forEachFunc(std::get<Seq>(std::forward<Tuple>(tuple)))...))
    {
        return func(forEachFunc(std::get<Seq>(std::forward<Tuple>(tuple)))...);
    }

    template<typename Func, typename ForEachFunc, typename... Args>
    static auto apply(Func&& func, ForEachFunc&& forEachFunc, std::tuple<Args...>&& args)
        -> decltype(invoke(std::forward<Func>(func),
                           std::forward<ForEachFunc>(forEachFunc),
                           std::forward<std::tuple<Args...>>(args),
                           typename GenerateSequence<sizeof...(Args)>::type()))
    {
        return invoke(std::forward<Func>(func),
                      std::forward<ForEachFunc>(forEachFunc),
                      std::forward<std::tuple<Args...>>(args),
                      typename GenerateSequence<sizeof...(Args)>::type());
    }
};

template<typename Func, typename ForEachFunc, typename Tuple>
inline auto invokeWithMemberFromAll(Func&& func, ForEachFunc&& forEachFunc, Tuple&& tuple)
    -> decltype(TupleForEachInvoker::apply(std::forward<Func>(func),
                                           std::forward<ForEachFunc>(forEachFunc),
                                           std::forward<Tuple>(tuple)))
{
    return TupleForEachInvoker::apply(std::forward<Func>(func),
                                      std::forward<ForEachFunc>(forEachFunc),
                                      std::forward<Tuple>(tuple));
}

// exemplar

template<typename T>
struct Foo
{
    T& data() { return _val; }
    T _val;
};

struct Extract
{
    template<typename T>
    T& operator() (Foo<T>& f) { return f.data(); }
};

int main()
{
    Foo<int>         i { 5 };
    Foo<double>      d { 6. };
    Foo<const char*> s { "hello world" };

    auto cb = [](int& i, const double& d, const char* s)
        {
            std::cout << "i=" << i << ", d=" << d << ", s=" << s << std::endl;

            i += 2;
        };


    // rvalue reference to tuple
    invokeWithMemberFromAll(cb, Extract{}, std::tie(i, d, s));

    std::cout << i.data() << std::endl;

    // lvalue reference to tuple - fails
    auto tuple = std::tie(i, d, s);
    invokeWithMemberFromAll(cb, Extract{}, tuple);

    std::cout << i.data() << std::endl;

}
4

2 回答 2

5
template<typename Func, typename ForEachFunc, typename... Args>
static auto apply(Func&& func, ForEachFunc&& forEachFunc, std::tuple<Args...>&& args)

第三个参数是 rvalue tuple,不是转发引用的元组,也不是对元组的转发引用。

通用引用(目前在开发中的 C++1z 标准中称为“转发引用”)通常需要推导类型。它们确实要求&&应用的类型可以是引用类型,也可以是非引用类型。Astd::tuple<?>始终是非引用类型,因此&&在放置时使其成为右值引用,而不是转发引用std::tuple<?>&&

看来您无缘无故切换Tuple&&到. 如果我是对的,那么等于。std::tuple<?>&&Args...std::tuple_size<std::decay_t<Tuple>>{}sizeof...(Args)

于 2015-02-24T22:11:26.933 回答
2

问题出在你的apply函数上——它需要一个rvalue类型std::tuple<Args...>

template<typename Func, typename ForEachFunc, typename... Args>
    static auto apply(Func&& func, ForEachFunc&& forEachFunc, std::tuple<Args...>&& args)

std::tuple_size这是使用而不是sizeof...(Args)来自您的代码的一种可能的修复方法:

struct TupleForEachInvoker
{
    template<typename Func, typename ForEachFunc, typename Tuple, size_t... Seq>
    static auto invoke(Func&& func, ForEachFunc&& forEachFunc, Tuple&& tuple, Sequence<Seq...>)
        -> decltype(func(forEachFunc(std::get<Seq>(std::forward<Tuple>(tuple)))...))
    {
        return func(forEachFunc(std::get<Seq>(std::forward<Tuple>(tuple)))...);
    }

    template<typename Func, typename ForEachFunc, typename Tuple>
    static auto apply(Func&& func, ForEachFunc&& forEachFunc, Tuple&& args)
        -> decltype(invoke(std::forward<Func>(func),
                           std::forward<ForEachFunc>(forEachFunc),
                           std::forward<Tuple>(args),
                           typename GenerateSequence<std::tuple_size<std::decay_t<Tuple>>::value>::type()))
    {
        return invoke(std::forward<Func>(func),
                      std::forward<ForEachFunc>(forEachFunc),
                      std::forward<Tuple>(args),
                      typename GenerateSequence<std::tuple_size<std::decay_t<Tuple>>::value>::type());
    }
};

template<typename Func, typename ForEachFunc, typename Tuple>
inline auto invokeWithMemberFromAll(Func&& func, ForEachFunc&& forEachFunc, Tuple&& tuple)
    -> decltype(TupleForEachInvoker::apply(std::forward<Func>(func),
                                           std::forward<ForEachFunc>(forEachFunc),
                                           std::forward<Tuple>(tuple)))
{
    return TupleForEachInvoker::apply(std::forward<Func>(func),
                                      std::forward<ForEachFunc>(forEachFunc),
                                      std::forward<Tuple>(tuple));
}

现场演示

于 2015-02-24T22:04:22.753 回答