'Recursive wrapper that adds a specific function applied to every objects in a container of container in c++

I would like to make a wrapper to a specific container (or a container of container) class such that the wrapper (1) inherits all the functions of the parent class and (2) adds a specific function that applies to all the elements.

For example, I want to add bar function that takes an object whose class is Destination on the container types that have bar(Destination). The first thing that comes to my mind is as below:

// I want as Enablebar<vector<Foo>> for any `Foo` that is guaranteed to have `void bar(Destination)`.
template <class Iterable> class Enablebar : public Iterable {
public:
    void bar(const Destination &dest) {
        for (auto &item : *this) {
            item.bar(dest);
        }
    }
};

So that I can enable bar functions on various containers in a project. (I cannot modify or touch the definitions of the containers, and there are many of such containers).

The problem is that I would like to use it also on container of container, or even container of container of container (i.e., vector<vector<list<Foo>>>), where Foo and vector are classes that I cannot touch or modify. So I just use it like Enablebar<vector<Enablebar<vector<Enablebar<list<Foo>>>>.

Question: is it possible to write a somewhat recursive, smart wrapper that replaces Enablebar that I can use something like: EnablebarSmartly<vector<vector<list<Foo>>>>? I'm not having a problem with EnableBar; just out of curiosity.

c++


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source