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 ?
The C++ committee is currently looking into "overloaded operator .
" for future revisions of the language.
However, in your specific case, you can simply inherit from the type.
template <typename T>
class Container : private T {
public:
using T::something_publicly_accessible;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With