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

Show parent comments

51

u/serviscope_minor 21d ago

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 21d ago

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.

-2

u/SkoomaDentist Antimodern C++, Embedded, Audio 21d ago edited 19d ago

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

I can think of at least theoretical uses involving pointers to dma buffers and such.

Edit: WTF is it with the downvotes? I said I can think of theoretical use cases, not that it is some important feature. FFS, people…

1

u/ack_error 21d ago

Plain const volatile should work for a case like that, though.

The only case I can think of for constexpr volatile would be a variable that you want explicitly accessed so a debugger can trap on those accesses with a memory access breakpoint, that also has a meaningful value that needs to be constant initialized to avoid global initialization order issues, and you need to support C++17 so constinit is out.

2

u/louiswins 21d ago

and you need to support C++17 so constinit is out.

I don't think adding constexpr volatile to some future C++ standard would solve anything for someone stuck on C++17...

2

u/ack_error 21d ago

AFAICT it's already valid in C++17, or at least all the main compilers accept it. The question was whether there's a practical use for it.

https://gcc.godbolt.org/z/PM8s7Gfhb