I have a template
T template<typename T>undefined_behavior(void)
{
throw;
return T{};
}
template<typename T>
struct UBHandler
{
const bool valid;
T value;
(operator T)()
{if(valid)return value;return undefined_behavior<T>();}
};
This works when using rvalues but breaks when using rvalue references because—references aren't pointers and can't be constructed out of thin air.
error: member ‘... ::value’ is uninitialized reference
Before I wipe out all code relying on this and start over, is there a way to satisfy the compiler with some kind of null reference or magic uncorn—union thing?
Or something.
Undefined behavior welcome.
This should work because std::optional exists and std::optional has rvalue reverence overloads? sadness. Perhaps not, after reading previously asked questions. But it was proposed in 2014 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3982.html I have no idea how to interpret this into a solution.
What is std::optional doing to get away with this that I'm missing? Sadness.
I tried reading the following questions but I have no ideas.