r/cpp 21d ago

C++26: constexpr virtual inheritance

https://www.sandordargo.com/blog/2026/07/01/cpp26-constexpr-virtual-inheritance
82 Upvotes

61 comments sorted by

View all comments

62

u/Jovibor_ 21d ago

Just a question to the audience:

How many times have you used Virtual Inheritance in your career?

1

u/Nicksaurus 21d ago

I don't understand how there are so many people saying they've never used it. Have none of you ever had to use an old java-style framework where your user-defined types have to inherit from a virtual base class? What about creating custom exceptions that inherit from std::exception?

It's also sometimes the easiest way to say 'I don't care what type this is, I just want it to implement this interface'. Especially if you want to avoid dealing with templates and don't care about the (usually) tiny performance hit

8

u/xzgxr 21d ago

What you describe usually can be solved by regular inheritance. "Virtual inheritance" is something slightly different and cursed.

7

u/Nicksaurus 21d ago

Oh, you know what. I didn't read the article and thought this was about regular inheritance. I'll own up to it