Идеально подходит для пересылки шаблонов с переменным l-значением ref и r-value ref и non-ref? - PullRequest
0 голосов
/ 28 февраля 2019

В моей программе у меня есть шаблоны классов, которые в основном являются обертками для специального назначения std :: function <..>.Минимальный пример:

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_;

};

Реализация этих шаблонов обычно представляет собой комбинацию типов l-значений ref, r-значений ref или no-ref.Проблема заключается в том, что вызов Bar приводит к ошибкам, когда некоторые из аргументов не являются ссылочными типами, такими как int или std :: vector.Обходной путь - объявить временную переменную и затем переместить ее в вызов функции.

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;
}

Я хочу иметь возможность использовать Bar с любым параметром шаблона без необходимости повторного присваивания и std :: move () каждый раз, но также иметь параметры ref, идеально перенаправленные.Есть ли способ сделать это?

РЕДАКТИРОВАТЬ После небольшого осмотра в Интернете - Проблема в том, что std::function<void(Args&&...)> function_; не является функцией, которая принимает универсальную ссылку, а вместо этого принимает ссылку на r-val.Поэтому попытка пересылки типов без ссылок выдает ошибку.

Таким образом, вопрос в том, возможно ли иметь и хранить std :: function, которая принимает универсальные ссылки?

1 Ответ

0 голосов
/ 28 февраля 2019

В std::function<void(Args&&...)> вы действительно ожидаете ссылку на r-значение, вы, вероятно, хотите 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_;
};

Демо

При необходимости вы можетеизбавиться от 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}; }

Демо

...