How to make parent template method visible from child class?

Here is a sample code:

#include <memory> class A { public: template <class T> void f(std::unique_ptr<T>, int) {} private: virtual void f(int) = 0; }; class B: public A { public: using A::f; private: virtual void f(int) override {} }; int main() { std::unique_ptr<float> a; B* b = new B; b->f(std::move(a), 1); return 0; } 

When I compile it with clang ++, I get an error message:

 'f' is a private member of 'A' using A::f; ^ 

How to make the template method f(std::unique_ptr<T>, int) visible from class B ?


Note : if the virtual method A::f(int) moved to a public section, everything works fine.

+3
c ++ c ++ 11 templates clang ++
source share
2 answers

Giving him a great name from a completely unrelated private virtual f .

The presence of an overloaded function in which different overloads have different levels of access control does not work and does not make sense. You should use the same name if the functions actually perform the same (or comparable given arguments), but it makes no sense to make one public and one personal.

If you have a public shell over a private virtual one (as usual, see, for example, std::basic_streambuf ), just add the appropriate prefix / suffix for one of them ( std::basic_streambuf uses the pub for the public, but it usually makes sense to add priv , impl or _ for a private virtual.

+3
source share

You cannot use using for this, since using cannot distinguish between template and virtual f . What makes the job a simple forwarder:

 class B: public A { public: template <class T> void f(std::unique_ptr<T> p, int i) { A::f<T>(std::move(p),i); } private: virtual void f(int) override {} }; 
0
source share

All Articles