简体   繁体   中英

Perfect forwarding variadic templates of l-value ref and r-value ref and non-ref types?

Im my program I have template classes that are mostly wrappers for a special purpose std::function<..>. The minimal example is:

template <typename... Args>
class Foo {

    public:

        explicit Foo(std::function<void(Args&&...)> _function)
            : function_(_function)
        {}

        template<typename... Arguments>
        void Bar(Arguments&&... _args) {
            function_(std::forward<Arguments>(_args)...);
        }

    private:

        std::function<void(Args&&...)> function_;

};

Instantiations of these templates are usually a combination of l-value ref, r-value ref or no-ref types. The problem is that calling Bar leeds to errors when some of the arguments are non-ref types such as an int or std::vector. The work around is to declare a temp variable and then move it into the function call.

int main(){
    Foo<int> test1([](int x) { });
    const int x = 1;
    test1.Bar(x); // [Error] cannot bind rvalue reference of type 'int&&' to lvalue of type 'const int'

    int tmp = x;
    test1.Bar(tmp); // [Error] cannot bind rvalue reference of type 'int&&' to lvalue of type 'int'
    test1.Bar(std::move(tmp)); // [OK] But I don't want to have to reassign and move every time I use this.

    /* I want perfect forwarding on variables that can be forwarded. */
    /* There are cases when the templates are like this with a combination of l-value ref and r-value ref and non-ref types. */
    Foo<const std::vector<uint8_t>&, std::vector<uint8_t>&&, int> test2([](const std::vector<uint8_t>&, std::vector<uint8_t>&&, int) { });
    test2.Bar(std::vector<uint8_t>(1, 2), std::vector<uint8_t>(1, 2), x); // [Error] cannot bind rvalue reference of type 'int&&' to lvalue of type 'const int'

    return 1;
}

I want to be able to use Bar with any template parameter without having to re-assign and std::move() everytime, but also have ref parameters perfectly forwarded. Is there a way to do this?

EDIT After looking around the web for a bit - The problem is std::function<void(Args&&...)> function_; is not a function that takes a universal ref but instead takes an r-val ref. So trying to forward no-ref types throws an error.

So the question then is, is it possible to have and store a std::function that takes a universals references?

In std::function<void(Args&&...)> , you actually expect r-value reference, you probably want std::function<void(Args...)> :

template <typename... Args>
class Foo {
public:
    explicit Foo(std::function<void(Args...)> _function)
        : function_(_function)
    {}

    template <typename... Arguments>
    void Bar(Arguments&&... _args) {
        function_(std::forward<Arguments>(_args)...);
    }

private:
    std::function<void(Args...)> function_;
};

Demo

If appropriate, you may get rid of std::function :

template <typename F>
class Foo {
public:
    explicit Foo(F f) : f(f) {}

    template <typename... Ts>
    auto operator ()(Ts&&... args) const
    -> decltype(f(std::forward<Ts>(args)...))
    {
        return f(std::forward<Ts>(args)...);
    }

private:
    F f;
};

template <typename F>
Foo<F> MakeFoo(F f) { return Foo<F>{f}; }

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM