0

When inheriting privately from a base class, I can say public: using Base::member; to make that (non-private) member public within the inheriting class. Is there some way to expand this to get all the members at once? The context I want this in is a CTRP setting I'm using to avoid code duplication.

Example:

struct Base {
  int foo();
  void bar();
  // ...
}

struct A : private Base {
  using Base::*; // not correct syntax

  // should have the effect of
  using Base::foo;
  using Base::bar;
  // ...
};

Even better would be if I could select only Base's public members, but not its protected or private members. This would just be a bonus.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • 3
    Nope, there's nothing in C++ like this. – Sam Varshavchik May 20 '21 at 12:28
  • Why can't you use `public` inheritance? – janekb04 May 20 '21 at 13:23
  • @janekb04 Defensive design? – bitmask May 20 '21 at 13:46
  • Could you elaborate more? I'm not sure how would using `private` inheritance be more defensive if you plan to achieve the same effect as with `public` inheritance anyway. If you're defending against changes to `Base`, than inheriting from it is already a cause of potential ABI breaks when `Base` is modified. You could use composition and store a pointer to `Base` if you're concerned with that, though admittedly in that case you would have to provide wrappers for its members manually. Do you want to prevent upcasting do `Base`? – janekb04 May 20 '21 at 13:58
  • @janekb04 I suppose the most pressing would be slicing. But I suppose there are many more design issues. – bitmask May 20 '21 at 21:19

0 Answers0