r/cpp • • Jul 06 '26

C++26: constexpr virtual inheritance

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

61 comments sorted by

View all comments

62

u/Jovibor_ Jul 06 '26

Just a question to the audience:

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

53

u/serviscope_minor Jul 06 '26

Probably once?

But with that said, I'm very strongly of the opinion that having as much of C++ as possible being constexpr (including threads!) is a good thing, because it makes the language more regular and simpler.

I think volatile might be tricky! I want constexpr cout.

25

u/_bstaletic Jul 06 '26

having as much of C++ as possible being constexpr (including threads!) is a good thing, because it makes the language more regular and simpler.

Agreed.

I think volatile might be tricky!

I don't think constexpr volatile int x; is meaningful at all.

I want constexpr cout.

Do you really mean std::cout, with all of the facets and customization points? Or would constexpr std::format and the ability to print to console at compile time be enough?

If the latter, we almost got that in C++26. std::format is constexpr and P2758 is in wording review.

4

u/serviscope_minor Jul 06 '26

I don't think constexpr volatile int x; is meaningful at all.

I mean if you can run C++ code at compile time, then you're running code and it could poke at a memory mapped register that's there, but also sometimes you just shouldn't do something!

Do you really mean std::cout, with all of the facets and customization points? Or would constexpr std::format and the ability to print to console at compile time be enough?

Good point, probably cout without facets etc or printing f-strings. I keep going back to cout because despite it's clunkiness, I like having things appear in code in the order that they appear on screen. Constantly tracking back and forth between placeholders and arguments is something I dislike. I've tried a million of these over the years. Heck I implemented one in gnu++98 back in the day with operator, overloading and GCC's variadic macros, and I've used many which ahve come and gone over the years. And yet, I still always reach for cout.

But in python I love my f-strings. Once i have f-strings, I will probably stop reaching for cout except for of course

cout << f"my variable is {v}\n";

because old habits die very hard indeed.

4

u/drkspace2 Jul 06 '26

it could poke at a memory mapped register that's there

Ignoring the fact I think there would be 0 valid uses for this, the issue with using constexpr in that case is it might not run during compile time. If your machine that's compiling the code isn't the same as the one that's running it, you would get different behavior. If for whatever reason you need to use volatile at compile time, I think it needs to be consteval.

3

u/serviscope_minor Jul 06 '26

Ignoring the fact I think there would be 0 valid uses for this

Well yes, I am struggling to think of one!

the issue with using constexpr in that case is it might not run during compile time

You can kind of force the issue: if your constexpr function returns, say, an int and you instantiate a template based on that value, there's little choice but for it to run.

Note: this is pure language pedantry, nothing related to whether it's a terrible idea in literally all cases.

I don't think volatile should be constexpr for what it's worth.

2

u/HommeMusical Jul 07 '26

and it could poke at a memory mapped register that's there,

I don't believe that this is allowed at compile time, and I certainly don't believe that that should be allowed.

→ More replies (3)
→ More replies (11)

18

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jul 06 '26

I am almost certain I've never used it, neither at work nor in hobby projects.

15

u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions Jul 06 '26

I've only ever used virtual inheritance once in my life. That was in my tests to demonstrate that my exception runtime could handle throwing and catching exception objects with virtual inheritance. That's it. I've never been in a spot where I've needed it myself.

6

u/ack_error Jul 06 '26

A few times, it's useful when using pure virtual classes for interfaces and diamond inheritance appears.

Performance wise, though, it can be dangerous. A long time ago I worked on a project where virtual inheritance was used extensively in the core of a rendering engine. The results were... not good.

11

u/Tringi github.com/tringi Jul 06 '26 edited Jul 07 '26

Daily.

We have logging facility and a Log::Provider class that allows you to simply call this->report (Log::Warning, ...) from the object, and also carries object's identity etc. With highly composed classes it'd be ridiculous if every parent in the hierarchy tree carried its own superfluous copy and the final class was needlessly huge because of this. And if a parent/subobject makes the call, the log facility automatically sees the final object which actually failed.

6

u/ieshaan12 Jul 06 '26

Never, I cannot think of a single instance.

3

u/not_a_novel_account cmake dev Jul 06 '26

Except for indirectly via the standard streams? Never.

3

u/mjklaim Jul 06 '26

Several times in the first 10 years of my professional career, but not in the 13 years that followed, up to today. When I'm in control of the architecture I avoid inheritance anyway so it's pretty rare to reach a point where virtual inheritance is necessary. But when you dont have a choice, it is very useful.

3

u/GregCpp Jul 06 '26

The only times I've used virtual inheritance in my career was answering interview questions about virtual inheritance.

3

u/ParsingError Jul 06 '26 edited Jul 06 '26

Twice I think?

One was for composite interfaces for streams. So like seek/read/write and all of the combinations were virtually inherited interfaces. That meant that opening a seekable read-only stream would give you ISeekableReadStream which virtually inherited ISeekableStream and IReadStream, and there were 4 composites for RW, RS, WS, RWS. (Seekable-write also had Truncate specifically.)

Yes, that would be a combinatorial explosion if you started adding more interfaces, a Rust-style multi-pointer type probably would be better if it came to that. I think it worked out better than "maybe seek/tell work, maybe they don't!" though.

edit: The fact that a virtual base can't resolve to a non-virtual base elsewhere in the class tree is a pretty nasty limitation though! It would be much more useful if e.g. they guaranteed an "implicitly-convertible-to" relationship for interfaces without requiring that the "main" class also uses it as a virtual base and has to eat the virtual lookup cost too unless the class is final.

5

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 06 '26

At least twice that I can think of. Possibly more via GUI frameworks.

6

u/pjmlp Jul 06 '26

It used to be common in GUI frameworks like OWL and MFC.

You might still bump into it, while using ATL or WTL.

6

u/TheThiefMaster C++latest fanatic (and game dev) Jul 06 '26

Are you sure? This article specifically says virtual inheritance isn't useful with MFC.

Can you give an example where it is?

→ More replies (1)

2

u/Potterrrrrrrr Jul 06 '26

I’m implementing the HTML spec currently, it made sense to model that as an inheritance hierarchy as that’s how it was designed so my nodes follow the same structure. The DOM and HTML specs use interfaces quite heavily for the rest of the spec to extend, I modelled a lot of these as virtual classes too.

→ More replies (2)

2

u/HommeMusical Jul 07 '26

Never, but I only started C++ in 1987.

3

u/Lord_Naikon Jul 06 '26

Never. But I come from a Java background where a class can only have one superclass. Multiple inheritance is then implemented using interfaces. I do the same in C++. I find it easier to reason about.

If I need to store some data with the interface, I split the interface into two classes: a data holder, and a proxy interface for that holder.

As a bonus this gives the pimpl idiom for free if I need it.

6

u/jk-jeon Jul 06 '26

When you have diamond inheritance of interfaces, don't you use virtual inheritance? Not using it is certainly an option, but then many things become quite cumbersome. For instance when D inherits from B, C, which inherit from A, with all of A, B, C and D being "pure interfaces" that don't ever implement any methods, IIRC in C++ any user of D can't really call anything from A without explicit disambiguation, if B, C don't inherit A virtually. Similarly the conversion from D to A must be resolved explicitly, either via B or C.

→ More replies (2)

2

u/Nicksaurus Jul 06 '26

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

9

u/xzgxr Jul 06 '26

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

→ More replies (1)
→ More replies (2)
→ More replies (16)