Conditionally inherit from a pure base class

Suppose I have the following class definitions

struct base { virtual int f() = 0; }; struct A: public base { int f() final { return 1; } }; struct B: public base { int f() final { return 2; } }; 

Is it possible to turn A and B into patterns that accept a bool parameter that indicates whether to inherit from base or not? I have use cases that fulfill or do not require a base class providing a common interface.

Assume that A and B have many member functions, so duplicating the implementation will be tedious. But sizeof(A) and sizeof(B) are small.

+5
source share
3 answers

I came up with a more direct approach that I was looking for, without duplicating the code.

 struct base { virtual int f() = 0; }; struct empty_base { }; template <bool Inherit> struct A final: public std::conditional_t<Inherit,base,empty_base> { int f() { return 1; } }; 
+3
source

Sure:

 template <bool> struct A { // ... }; template <> struct A<true> : base { // ... }; 

(Note that you can make A<true> from A<false> if this avoids redundancy.)

For instance:

 template <bool> struct A { void f() { std::cout << "A::f called\n"; } }; template <> struct A<true> : A<false>, base { void f() override { A<false>::f(); } }; int main() { A<false> a1; A<true> a2; a1.f(); a2.f(); static_cast<base&>(a2).f(); } 
+4
source

Since you are using a pure base class, the difference should not be important, since your optimizer will avoid calling a virtual function when calling A::f() , since there will never be a derived class that implements another version of f() .

You can also make class A final : base , if you do not plan to inherit from A , so as not to add final to each function.

+1
source

Source: https://habr.com/ru/post/1262781/


All Articles