lundi 15 octobre 2018

How to make a constructor with variadic and put convert it in the vector?

I have found this solution. It works but I want to my class was owner of arguments. I have the next code:

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

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

        return true;
    }
};

I need to I could write a something like below:

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

I.e. to I could return my exp_t and it saved passed arguments.
How can I change my class and_t? Is it possible in C++?
P.S. I tried to get by myself reading about variadics but I understood a nothing just.

Aucun commentaire:

Enregistrer un commentaire