【发布时间】: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