完美转发失败的左值

时间:2015-02-24 21:48:56

标签: c++ c++11 perfect-forwarding stdtuple

我有一个实用函数,它迭代一个元组,对于每个元素,用该元素调用一个函数,最后用所有元组元素的结果调用另一个函数。

为了更好地说明:

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

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

问题:完美转发

我尝试使用所谓的“通用引用”和完美转发来实现该实用程序,从而无需多个左值/右值和const /非常量重载。

虽然函数的参数是

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

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

我问了一个类似的问题,导致这个here已经解决了;但是,由于这是一个无关的问题,我认为这需要一个新的问题

关于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;

}

2 个答案:

答案 0 :(得分:5)

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

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

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

除了计算Tuple&&的数量之外,你似乎没有任何理由从std::tuple<?>&&切换到Args...。如果我是对的,那么std::tuple_size<std::decay_t<Tuple>>{}等于sizeof...(Args)

答案 1 :(得分: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));
}

Live Demo

相关问题