This is one case that cannot be a strictly idiom, but is similar enough to know about it. That is, to have a type template wrapper through a non-type version.
class MapBase { public: void* getForKey(const std::string & k); void setForKey(const std::string & k, void * v); ... }; template<typename T> class MyMap { public: T* getForKey(const std::string &k) { return (T*)base_.getForKey(k); } void setForKey( const std::string &k, const T* v) { base_.setForKey(k, T*v); } private: MapBase base_; };
Now, any use of MyMap<T> should not be exposed to the internal properties of MapBase, and you get only one implementation of these functions. I would also consider making MapBase an abstract base class to make the decoupling even stronger.
As I said, this is not exactly a simplex, but it solves the same problems the same way.
source share