存储稍后转发的变量参数

2023-12-29

我正在开发一个小程序,它使用操作队列顺序执行操作。

我希望能够在我的操作中存储参数,直到它们被执行(然后应该可以从exec()动作的方法)。

我下面有一个小例子:

#include <tuple>
#include <iostream>
#include <memory>
#include <utility>
#include <queue>

/**
 * Action interface
 */
struct Action {
    Action() {}
    virtual void exec() = 0;
};


/**
 * This action creates an object which type is given as template
 * and passes the parameters given to its ctor. On completion, it
 * prints its ID.
 */
template<class T, class... Ps>
struct CustomAction : public Action {
    // trying to store the variable arguments
    std::tuple<Ps...> _args;
    int _actionId;

    CustomAction(int id, Ps&&... args) : _actionId(id), 
        _args(std::make_tuple(std::forward<Ps>(args)...)) {
    }

    virtual void exec() override {
        T* item = new T(std::forward<Ps>(_args)...);

        std::cout << "done " << _actionId << std::endl;
    }
};

struct ActionQueue {

    std::queue<Action*> _queue;

    ActionQueue() {

    }

    void exec() {
        while(_queue.size()) {
            auto action = _queue.front();
            action->exec();
            _queue.pop();
        }
    }

    template<class T, class... Ps>
    void push(Ps&&... args) {
        auto action = new T(std::forward<Ps>(args)...);

        _queue.push(action);
    }
};

/**
 * Example item that is to be created. Nothing important here
 */
struct Item {

    int _b;

    Item(int b) : _b(b) {

    }
};

int main() {


    ActionQueue aq;

    int actionId = 5;
    int param = 2;

    aq.push<CustomAction<Item>>(actionId, param);

    // do stuff

    aq.exec();
}

在这个例子中,我创建了一个ActionQueue。我推一个新的CustomAction在队列中。这个动作只是创建一个Item并向其构造函数提供我在将操作推送到操作队列时给出的参数。

我的问题是我不知道为什么给的参数push()方法不可用于CustomAction class.

编译上面的例子给出了以下错误:

<source>:56:27: error: no matching constructor for initialization of 'CustomAction<Item>'

        auto action = new T(std::forward<Ps>(args)...);

                          ^ ~~~~~~~~~~~~~~~~~~~~~~

<source>:82:8: note: in instantiation of function template specialization 'ActionQueue::push<CustomAction<Item>, int &, int &>' requested here

    aq.push<CustomAction<Item>>(actionId, param);

       ^

<source>:27:5: note: candidate constructor not viable: requires single argument 'id', but 2 arguments were provided

    CustomAction(int id, Ps&&... args) : _actionId(id), 

    ^

<source>:22:8: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided

struct CustomAction : public Action {

       ^

<source>:22:8: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided

1 error generated.

Compiler returned: 1

错误说CustomAction需要一个参数,而给出了两个参数,但是CustomAction应该接受第二个参数args.

我在这里做错了什么?

Thanks


我在这里做错了什么?

好吧...首先你需要解压元组;例如,使用辅助函数,如下所示(std::index_sequence and std::make_index_sequence从 C++14 开始可用;你正在编译 C++17 所以你可以使用它们)

template <std::size_t ... Is>
void exec_helper (std::index_sequence<Is...> const &)
 { T* item = new T{std::get<Is>(_args)...}; }

virtual void exec() override {
    exec_helper(std::make_index_sequence<sizeof...(Ps)>{});

    std::cout << "done " << _actionId << std::endl;
}

但还有其他问题。

例如:您只能对函数/方法的模板参数使用完美转发,因此不能在此构造函数中使用

CustomAction(int id, Ps&&... args) : _actionId(id), 
    _args(std::make_tuple(std::forward<Ps>(args)...)) {
}

because Ps...是类的模板参数,而不是构造函数的模板参数。

应该像这样工作

template <typename ... Us>
CustomAction (int id, Us && ... args) : _actionId{id}, 
    _args{std::forward<Us>(args)...}
 { }

我想

aq.push<CustomAction<Item>>(actionId, param);

应该

// ........................VVV
aq.push<CustomAction<Item, int>>(actionId, param);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

存储稍后转发的变量参数 的相关文章

随机推荐