【问题标题】:How to convert variadic in the vector to it was a holder of arguments?如何将向量中的可变参数转换为参数持有者?
【发布时间】:2019-03-20 08:57:09
【问题描述】:

我找到了this solution。它有效,但我希望我的班级是争论的主人。我有下一个代码:

template <class val_t>
class exp_t {
public:
    exp_t() {}
    virtual ~exp_t() {}

    virtual bool is(const std::vector<val_t> &kb) const = 0;
};

template <class val_t>
class fact_t: exp_t<val_t> {
public:
    const val_t m_value;

    fact_t(const val_t value): m_value{value} {}

    virtual bool is(const std::vector<val_t> &kb) const {
        return std::find(kb.begin(), kb.end(), m_value) != kb.end();
    }
};

template <class val_t>
class not_t: public exp_t<val_t> {
    exp_t<val_t> m_exp;
public:
    not_t(exp_t<val_t> exp): exp_t<val_t>(), m_exp{exp} {}

    virtual bool is(const std::vector<val_t> &kb) const override {
        return !m_exp.is(kb);
    }
};

template <class val_t, class ... args_t>
class and_t: public exp_t<val_t> {
    std::vector<exp_t<val_t>> m_exps;
public:
    and_t(args_t... exps) : exp_t<val_t>(), m_exps{{exps...}} {}

    virtual bool is(const std::vector<val_t> &kb) const override {
        for (auto &exp : m_exps) {
            if (!exp.is(kb)) { return false; }
        }

        return true;
    }
};

我需要我可以写一个类似下面的东西:

exp_t<int> *get_exp() {  
    return new and_t<int>(fact_t<int>(5), fact_t<int>(6));
}

即我可以返回我的 exp_t 并保存传递的参数(例如使用移动语义,我知道如何使类可移动,但我不知道如何重写 and_t 构造函数以传递它并转换为 @987654326 @)。
如何更改我的班级and_t?在 C++ 中可能吗?

P.S.我试图自己阅读可变参数,但我什么都不懂。

【问题讨论】:

    标签: c++ c++11 variadic-templates move-semantics perfect-forwarding


    【解决方案1】:

    即我可以返回我的 exp_t 并保存传递的参数(例如使用移动语义,我知道如何使类可移动,但我不知道如何重写 and_t 构造函数来传递它并转换为 std::vector)

    如果您知道(如果您确定)所有参数都是 r 值,则可以使用如下移动语义

     and_t (args_t && ... exps)
        : exp_t<val_t>(), m_exps{{std::move(exps)...}}
      { }
    

    否则(如果某些参数可以是右值,某些左值),您可以使用完美转发

    template <typename ... Args>
    and_t (Args && ... exps)
       : exp_t<val_t>(), m_exps{{std::forward<Args>(exps)...}}
     { }
    

    所以你移动右值并复制左值。

    我想最好的方法是第二种方法(完美转发),因此and_t 类不需要args_t 可变参数类型列表。

    【讨论】:

    • 非常感谢!我将用std::forward 重写我的课程。
    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多