Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ method forwarding

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 ?

like image 393
Claudio Avatar asked Dec 02 '22 14:12

Claudio


1 Answers

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;
};
like image 65
Potatoswatter Avatar answered Dec 08 '22 00:12

Potatoswatter