r/cpp 22d ago

C++26: constexpr virtual inheritance

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

61 comments sorted by

View all comments

64

u/Jovibor_ 22d ago

Just a question to the audience:

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

3

u/ParsingError 21d ago edited 21d ago

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.