*get_if should be enough. Unconditionally dereference the result.
In theory, a portable __builtin_unreachable() would be:
[[noreturn]]
inline void unreachable() {} // basically, unconditionally trigger undefined behavior
In practice, only GCC seems to recognize this.
--Justin Bassett
[build] C:\Users\Ryan\rpnx-core\private\sources\all\test3.cpp(119,5): error C3861: '__builtin_unreachable': identifier not found [C:\Users\Ryan\rpnx-core\build\rpnx-core-test3.vcxproj]
Great idea, except that __builtin_unreachable() is a GCC specific extension, and is not part of standard C++. (It would be nice to be able to do this in a cross platform way though! But that is for another discussion.)
--
Ryan P. Nicholl
Tel: (678)-358-7765
Tox: 52B3A61CDAF88BCC854F568933F071E3530600CDBA3D5354AADC9AD179575D68021AE959719D
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
I decided to implement a new class based on std::variant. I call it "rpnx::derivator", but it's basically "allocating_variant". I tried to make it as similar to std::variant as possible. When looking at this, I noticed something weird about std::variant. There is no "zero overhead" way to get the element contained by the variant, as std::get<I> checks for invalid access and throws an exception if invalid. To solve this issue, I would like to propose std::as, which works the same as std::get, but accessing the wrong type is undefined behavior instead of throwing an exception.
--
Ryan P. Nicholl
Tel: (678)-358-7765
Tox: 52B3A61CDAF88BCC854F568933F071E3530600CDBA3D5354AADC9AD179575D68021AE959719D
You can achieve this by using std::get_if() and marking the nullptr case as unreachable. For example:
auto f(std::variant<int, double>& v) -> int* {
return std::get_if<int>(&v);
}
auto g(std::variant<int, double>& v) -> int* {
auto p = std::get_if<int>(&v);
if (not p) __builtin_unreachable();
return p;
}
Barry
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals