The example in the paper is implementing optional<bool> so that it is fully usable during constant evaluation with the same interface, but still taking only 1 byte.
It's a very narrow facility that simply exposes information the compiler already has to have, in a way that predates reflection (I wrote R0 in 2022). A more reflection-y API would probably take a union* and return a reflection representing which non-static data member is active, or take a T* and return a reflection representing the type of the complete object alive at that spot. Haven't really thought about it in enough detail to square those two (which might just be different functions).
It surprises me that even in R0, which I hadn't read until just now, you never ask that the language just stops outlawing these actions at compile time.
There are several paths forward but the easiest to me starts here:
operator*() involves a reinterpret_cast, which is explicitly disallowed by [expr.const]/5.15.
5.15 is just one item from a long list of things C++ declines to do here, so I guess the "specific rule change" I am suggesting is to delete this item.
It's not so easy. For starters, there are plenty of reinterpret_casts that might be fine at runtime that we definitely don't want to allow during constant evaluation time — the one I mentioned in another comment for instance is converting a T* into a uintptr_t, since while we have a symbolic address, we don't know what the actual address will be at runtime. So we'd need to carefully go through precisely which reinterpret_casts we want to be able to allow.
That's also not enough anyway, since you'd also need to allow placement-new with a non-similar type — the alternate implementation is placement-newing a bool into an unsigned char. Currently, we only allow you to placement-new a T(~ish) onto a T*.
Opening up constant evaluation to operate on bytes instead of objects is a pretty significant change to the constant evaluation model. Maybe it's worth doing, but it's definitely not simply a matter of just removing a bullet somewhere.
to play devil's advocate, how are unions useful in constexpr? The things that make unions useful (saving space, easy bit casting) don't seem to be very useful at compile time, since your compile time union shouldn't appear at runtime
It looks like the motivation is to be able to reuse the same union-optimized runtime types in constexpr, instead of having to have separate optional and constexpr_optional types.
But optional already stores its state outside of the union. If you want to use the same optional implementation for both constexpr and runtime, doesn't has_value() already give you all the information you need?
The tricky part is that accessors like value() return a reference, so optional<bool> must contain an actual bool in it. Otherwise, it'd be simpler as it could just encode and decode from a plain char instead of needing union or casting machinery to combine the bool and optional state into a single byte.
3 years ago I tried to implement a alternative to std optional that made optional<bool> a single byte only. I've noticed it was impossible to make a constexpr one.
Genuinely, I want to understand .... Why would one implement optional<bool> as a union or as two bytes? It's trivially obvious how to implement a bool specialization of optional as 2 bits. The paper shows you how, using a char with a sentinel value. Is the desire to use a union or separate flag just to avoid having a bool specialization?
template<>
class optional<bool> {
public:
constexpr bool has_value() const { return value < 2; }
constexpr bool get() const { return value == 0 ? false : true; }
// ....
private:
uint8_t value = 0;
};
You lose some properties that optional normally has. The main one is that the get() returns a reference instead. With your implementation you can't have a reference to the bool value, while, that's achievable using unions
regarding OptBool, at runtime, checking c whether b is valid is undefined behavior.
either the b was used to write the value, in which case the c cannot be used to detect if b is valid, or the c was used to write the value, in which case the b cannot be used to return the value. the solution is to use c only, at which point the std::is_within_lifetime is not applicable in this example.
Could you elaborate what exactly is the difference between constexpr vs runtime in this case? Why it's not UB in runtime but we still can't do that in constexpr? Also why not bit_cast in constexpr?
Just because something isn't undefined behavior doesn't mean it's allowed during constant evaluation. For instance, you cannot convert a T* to a uintptr_t - that's not UB, but the actual integer address of that object isn't known at compile time, so you're not allowed to try to look at it.
why not bit_cast in constexpr?
You can, provided neither the source nor destination type have something in them whose representation isn't knowable at compile time. Like pointers.
Currently, that includes unions, too. I do wonder if we could relax that restriction in the future. At the very least, bit_cast-ing FROM a union whose active member takes the whole space should probably be fine? bit_casting TO a union might still be problematic though. Will take more consideration. Plus it's nice right now that the rule is symmetrical.
But the example code doesn't explicitly do that, does it? Wouldn't one need to actually cast a pointer to a char* to achieve that?
Also, while it's incredibly unlikely, isn't it possible for an implementation to store true as the same bit pattern as 2? I mean I know that implicit conversions convert true as 1. But that's not the same as the actual bit representation. Casting to char* wouldn't engage the same implicit conversion logic.
Then you pick a different value that's not a valid pattern.
If every pattern is valid, then you don't get to use the optimization, but that's fine. This is no different from code assuming endianness, 8-bit bytes, etc., that people write all the time.
At runtime, you can track the active member with a sentinel value in c — for example, using 2 to indicate “no value” since bool only uses 0 or 1. But during constant evaluation, this becomes problematic. The compiler needs to know which union member is active without relying on runtime tricks.
Is there an explanation for why the runtime trick can't be used here? This sounds like a completely valid thing to do at first glance
The bit layout of a union is not specified at compile-time. People often use common compiler extensions that define the behavior of accessing invalid members of a union at runtime. Functions such as std::bit_cast that can be used to do this without undefined behavior are runtime-only for certain types such as unions:
This function template is constexpr if and only if each of To, From and the types of all subobjects of To and From: ... is not a union type;
55
u/cleroth Game Developer Feb 21 '26
I want to see a concrete example of how this is actually useful.
23
u/trad_emark Feb 21 '26
regarding OptBool, at runtime, checking c whether b is valid is undefined behavior.
either the b was used to write the value, in which case the c cannot be used to detect if b is valid, or the c was used to write the value, in which case the b cannot be used to return the value.
the solution is to use c only, at which point the std::is_within_lifetime is not applicable in this example.
3
u/max123246 Feb 21 '26
Is there an explanation for why the runtime trick can't be used here? This sounds like a completely valid thing to do at first glance
2
u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions Feb 22 '26
Neat. I remember seeing this, but didn't look into it much. Thanks for the article!
-6
u/vI--_--Iv Feb 21 '26
Oh, but why stop here?
Eight commands are enough for Turing-completeness, so we should generalize more.