What you really want is a type among the alternatives which has a single possible value - not void
, which has no possible values (and is problematic in other ways). In other words: A unit type instead of a bottom type.
The standard library has defined, as part of <variant>
, a "unit type" for this use case: std::monostate
(and yes, it's essentially an empty struct). Use it.
Example:
#include <variant>
using Foo = int;
using Bar = double;
int main() {
std::variant<std::monostate, Foo, Bar> v;
v = Foo{};
}
Note that, unlike in the question, the single-possible-value type is the first alternative; this allows the variant to be default-constructible even if Foo
isn't. Also, it's potentially cheaper/faster to construct the variant this way than constructing a Foo
, even if it is default-constructible.
struct { };
as replacement ofvoid
is not that new. New (for me) is that it got part ofstd
library since C++17 (asstd::monostate
). ;-) – Decoder