Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a C++ templated function choose a member variable?

I would like a class to have a function with a template argument, and based on that template argument a particular member variable is manipulated.

For example, if function template specialization were allowed, then something like this:

struct A
{
    struct M1 {};
    struct M2 {};

    // Function template specialization not allowed :(
    template<typename M>
    void addM(M const &m);

    template<>
    void addM(M1 const &m)
    {
        m1_vec_.push_back(m);
    }

    template<>
    void addM(M2 const &m)
    {
        m2_vec_.push_back(m);
    }

    std::vector<M1> m1_vec_;
    std::vector<M2> m2_vec_;
};

Any ideas? I feel like I'm missing something simple but can't quite put my finger on it.

like image 838
user5406764 Avatar asked Oct 25 '25 16:10

user5406764


1 Answers

Just overload them :

struct A
{
    struct M1 {};
    struct M2 {};

    void addM(M1 const &m)
    {
        m1_vec_.push_back(m);
    }

    void addM(M2 const &m)
    {
        m2_vec_.push_back(m);
    }

    std::vector<M1> m1_vec_;
    std::vector<M2> m2_vec_;
};

If you do not wish to duplicate addM's code, you can just abstract the vector choice behind another function :

struct A
{
    struct M1 {};
    struct M2 {};

    template <class T>
    void addM(T const &m)
    {
        getVec<T>().push_back(m);
    }

    std::vector<M1> m1_vec_;
    std::vector<M2> m2_vec_;

private:
    template<class T>
    std::vector<T> &getVec();
};

template <>
std::vector<A::M1> &A::getVec() { return m1_vec_; }

template <>
std::vector<A::M2> &A::getVec() { return m2_vec_; }
like image 195
Quentin Avatar answered Oct 27 '25 05:10

Quentin