barney
barney

Reputation: 2202

perfect forwarding / move construction isn't working from tuple in std::apply

I'm trying to construct the perfect forwarded (zero copied) construction using nested lambda captures. I expect that there should be zero copy constructions, but something is broken.

I move from variadic arg. pack to tuple (moved ok) then I pass the tuple (moved ok) to the std::apply and in final nested lambda I assemble another tuple (expected to be moved ok but Wrapper CTOR is COPY not MOVE:

#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();
}

Gives an output CTOR MOVE-CTOR COPY-CTOR (1, 2, st, 42)

Upvotes: 3

Views: 208

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275500

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

change this to

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

args_upper is implicitly const unless you make your lambda mutable. That'll block move semantics.

Upvotes: 9

Related Questions