Extending a type in C++

2019-04-06 03:31发布

问题:

Sadly, UFCS did not make it into C++17 and that left me with a recurring problem: Sometimes I want to give types extra functionality using the method call syntax (without writing global functions). That would especially come handy when dealing with monads.

I see two options: One is inheritance, and the other is encapsulation. Because you cannot safely inherit from STL containers, that leaves encapsulation. For example, I want to extend std::optional, so I write:

template <typename T>
struct myoption {
    // Some functionality
private:
    std::optional<T> impl;
};

My problem is that every time I want to do this, I basically have to write all the constructors (and needed methods that you can use with the original type, like push_back for vectors) the original type has. Even a simpler container, like optional has 9 constructors. When using inheritance, I can simply 'inherit' the methods and constructors of a super-class. Is there a way to make this easier using encapsulation?

回答1:

I would implement it by using private inheritance:

#define MAKE_PUBLIC(method) using std::vector<T>::method

template <typename T>
struct My_vector : private std::vector<T> {
    MAKE_PUBLIC(push_back);
    MAKE_PUBLIC(pop_back);
};

int main() {
    My_vector<int> v;
    v.push_back(3);
    std::vector<int>* vec = new My_vector<int>; // won't compile
}

This way, you can make sure that you cannot create objects with dynamic type of My_vector and reduce the effort to make inherited methods accessible by a mere macro (or using directive) instead of creating forward functions for each member function and overload.