I need to implement a class Container which acts exactly as the contained template class:
template <typename T>
class Container {
public:
//...
private:
// T data_;
};
T
can be either a predefined type (e.g., int
) or a user-defined type.
The purpose is to intercept any read/write operations done on the contained type.
I've succesfully implemented most operators, and it works.
However, when I need to access methods specific of the contained class T, it doesn't work:
Container<myclass> a;
a.myclass_specific_method();
The reason is that Container obviously doesn't have such methods. Moreover, since T is a template, its methods cannot be known in advance.
I guess there is no solution to this problem, even with C++11, because operator .
cannot be overloaded. Therefore, the only possible approach is to always rely on operator->
like smart pointers do.
Can you confirm ?
For a class type
T
, this will act a lot like aT
:for a non-class
T
, we detect it and use a different implementation:finally, we go off and override every operator we can find in a SFINAE friendly way, where the operator only participates in overload resolution if
get_t(Container)
would work in its place in the operator. This should all be done in a namespace, so the operators are found via ADL. An overload ofget_t
that returns its argument unchanged could be useful to massively reduce the number of overloads.This could be another 100 or more lines of code.
Users of
Container<T>
can bypass theContainer<T>
and get the underlyingT
in the above system.