【问题标题】:perfect forwarding / move construction isn't working from tuple in std::apply完美的转发/移动构造不适用于 std::apply 中的元组
【发布时间】:2018-12-03 14:15:34
【问题描述】:

我正在尝试使用嵌套的 lambda 捕获构建完美的转发(零复制)构造。 我希望应该有零复制结构,但有些东西坏了。

我从可变参数 arg 移动。打包到元组(移动正常),然后我将元组(移动正常)传递给 std::apply 并在最终嵌套的 lambda 中组装另一个元组(预计移动正常,但 Wrapper CTOR 是复制而不是移动:

#include <iostream>

// tuple printer (ignore it)
template<typename Type, unsigned N, unsigned Last>
struct tuple_printer {
    static void print(std::ostream& out, const Type& value) {
        out << std::get<N>(value) << ", ";
        tuple_printer<Type, N + 1, Last>::print(out, value);
    }
};

template<typename Type, unsigned N>
struct tuple_printer<Type, N, N> {
    static void print(std::ostream& out, const Type& value) {
        out << std::get<N>(value);
    }
};

template<typename... Types>
std::ostream& operator<<(std::ostream& out, const std::tuple<Types...>& value) {
    out << "(";
    tuple_printer<std::tuple<Types...>, 0, sizeof...(Types) - 1>::print(out, value);
    out << ")";
    return out;
}

// THE FUNCTION that returns lambda:
template <class ... Args>
auto f(Args && ... args)
{
    // v--- args is a tuple<arg1, arg2, arg3 ...>
    return [args_upper = std::make_tuple(std::forward<Args>(args)...)]()
    {
        // v-- lower "args" is a restored list of arguments - moved from the upper tuple "args"
        return std::apply([](auto && ... args) {
            // v--- here the Wrapper COPY-CTOR is called instead of moved from
            return std::make_tuple(std::forward<decltype(args)>(args)...);
        }, std::move(args_upper));
    };
}

struct Wrapper {
    Wrapper() {
        std::cout << "CTOR ";
    }
    Wrapper(const Wrapper & r) {
        std::cout << "COPY-CTOR ";
    }
    Wrapper(Wrapper&& r) {
        std::cout << "MOVE-CTOR ";
    }
    int w = 42;
    friend std::ostream& operator<<(std::ostream& out, const Wrapper& w);
};

std::ostream& operator<<(std::ostream& out, const Wrapper& w) {
    out << w.w;
    return out;
}

int main() {
    auto l = f(1,2.f,"st", Wrapper{});
    auto t = l(); // t is tuple
    std::cout << t; // tuple printer
//    std::cout << l();
}

给出一个输出 CTOR MOVE-CTOR COPY-CTOR (1, 2, st, 42)

【问题讨论】:

  • 我认为这里的关键是decltype(args),这可能会导致引用类型被传递。但是std::forward 期望类名通常没有任何引用。你试过decltype(auto)吗?
  • @ThomasLang decltype(auto) 仅用于函数返回类型推导。我试过 std::forward<:remove_reference_t>>(args)...);但仍然会发生复制构造

标签: c++ templates lambda variadic-templates perfect-forwarding


【解决方案1】:
return [args_upper = std::make_tuple(std::forward<Args>(args)...)]()

改成

return [args_upper = std::make_tuple(std::forward<Args>(args)...)]() mutable

args_upper 隐含为const,除非您将 lambda 设为mutable。这会阻止移动语义。

【讨论】:

  • 成功了!你怎么知道?即这里考虑常量的一般策略是什么?即你是怎么知道的??))
  • @barney 请记住,当您按值捕获时,您无法更改值,因为 lambdas 隐式为 const,除非您将它们标记为 mutable。
  • @barney 根据未命名的结构来考虑 lambda,其中(默认情况下)const operator() 是给定的函数体,任何捕获都是成员。您可以通过添加 mutable 关键字从 operator() 中删除 const。
  • @MaxLanghof 是的,这就是为什么了解潜在的“物理”模型总是有用的——建立正确的心理形象“它是什么”和“它是如何在引擎盖下构建的”——看看那些微小的细节,比如operator()的const-ness
  • @barney 我想“好吧,你正在尝试移动,它正在复制。这可能意味着你搞砸了完美的转发——一些引用的左值性。嗯,不,看起来你有 l "像大多数事情一样,这是以前犯过错误的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 2015-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
相关资源
最近更新 更多