Arne
Arne

Reputation: 8481

polymorphic vector without virtual or inheritance

I am trying to implement a vector that can take elements of several types, and can apply a function on all of them. This is easily done with a base class, virtual functions and inheritance, but I explicity do not want to use it. Here is how far I am so far:

#include <iostream>
#include <vector>
#include <tuple>

// this will be my new polymorphic vector;
template<typename... Ts>
class myvector {
    std::tuple<std::vector<Ts>...> vectors;

    template <template<typename> class funtype>
    void for_each() {
    }

    template <template<typename> class funtype, typename X, typename... Xs>
    void for_each() {
        std::vector<X>& vector = std::get<std::vector<X>>(vectors);
        for ( X& x : vector ) {
            funtype<X> fun;
            fun(x);
        }
        for_each<funtype, Xs...>();
    }

public:
    template <typename T>
    void push_back(const T& t) {
        std::vector<T>& vector = std::get<std::vector<T>>(vectors);
        vector.push_back(t);
    }

    template <typename T>
    void pop_back() {
        std::vector<T>& vector = std::get<std::vector<T>>(vectors);
        vector.pop_back();
    }

    /* here I would like to pass a function, or function object that 
     * can be expanded to all underlying types. I would prefer to just 
     * give a function name, that has an implementation to all types in Ts
     */
    template <template<typename> class funtype>
    void ForEach() {
        for_each<funtype,Ts...>();
    }
};


struct foo {
};

struct bar {
};

template <typename T>
void method(T& t);

template<>
void method(foo& b) {
    std::cout << "foo" << std::endl;
}

template<>
void method(bar& b) {
    std::cout << "bar" << std::endl;
}

int main()
{
    myvector<foo,bar> mv;
    mv.push_back( foo{} );
    mv.push_back( bar{} );
    mv.ForEach<method>();
}

at the moment I am kind of stuck, I hope you can give me some advise on how to go further.

Upvotes: 2

Views: 469

Answers (1)

dyp
dyp

Reputation: 39131

A common solution is to use a function object with a set of operator():

struct my_fun_type
{
    void operator()(foo&) const
    { std::cout << "foo\n"; }

    void operator()(bar&) const
    { std::cout << "bar\n"; }
};

This allows to pass a "set" of overloaded functions to an algorithm, state, and is rather convenient to use:

my_algorithm(my_fun_type{});

If we want to add support for such function objects, we could define ForEach as follows:

template <typename Elem, typename Fun>
void for_each(Fun&& fun) {
    std::vector<Elem>& vector = std::get<std::vector<Elem>>(vectors);
    for ( Elem& e : vector ) {
        fun(x);
    }
}

template <typename Fun>
void ForEach(Fun&& fun) {
    int dummy[] = { 0, (for_each<Ts>(fun), 0)... };
    (void)dummy;
}

That dummy is a trick to call for_each for all types in Ts. The (void)dummy is intended to suppress a compiler warning (dummy is never read from).

You can learn more about this technique in other Q&As, such as that one.

The Fun&& is not an rvalue reference, but a universal reference.


Note that the above example differs from many Standard Library algorithms, which take the function object by value:

template <typename Elem, typename Fun>
void for_each(Fun fun) {
    std::vector<Elem>& vector = std::get<std::vector<Elem>>(vectors);
    std::for_each(vector.begin(), vector.end(), std::move(fun));
}

template <typename Fun>
void ForEach(Fun fun) {
    int dummy[] = { 0, (for_each<Ts>(fun), 0)... };
    (void)dummy;
}

To pass a set of overloaded free functions, we can wrap them in a function object (thank @Yakk for the suggestion):

struct method_t
{
    template<class... Ts>
    void operator()(Ts&&... ts) const
    { method( std::forward<Ts>(ts)... ); }
};

In C++1y, such a function object type can be created with less boilerplate using a polymorphic lambda:

[](auto&&... pp)
{ method( std::forward<decltype(pp)>(pp)... ); }

Upvotes: 3

Related Questions