Return type std::optional<std::variant<...>>
Asked Answered
P

2

23

I have a situation where a function must return a value taken from a table. A cell in this table (let's assume the table just works...) may contain a value, or it might not. This value can also be one of several types: int, double, string, date (but no other type).

What would such a function return? Is it a good idea to return std::optional<std::variant<std::string, int, double, std::chrono::time_point>>?

Would that be a good use of optional and variant?

Perceval answered 18/5, 2017 at 16:13 Comment(1)
sounds like optional + variant can solve this requirement.Excoriate
C
35

I would consider this to be a useful use of std::monostate. Specifically, variant<std::monostate, int, double, std::string, std::chrono::time_point>. monostate is useful for cases where a variant may not contain a value.

The nice thing about using an actual type rather than optional<variant> is that visitation works normally on it. You can write a functor that can take a monostate parameter, thus allowing you to use visit for even "empty" variants.

Cheerless answered 18/5, 2017 at 18:2 Comment(2)
"Unit type intended for use as a well-behaved empty alternative in std::variant. [...]." Sounds about right. en.cppreference.com/w/cpp/utility/variant/monostateOceanus
optional<variant<...>> will be false if empty and true if it is occupied, which can be handy.Myrmecophagous
S
4

Just want to add that before C++17 and the standardization of variant and monostate, there is already boost::blank to solve the exact same issue for boost::variant.

By convention, if boost::blank is used, it should always be the first template argument, so that a default-constructed variant is empty and checking for emptyness is done with .which() == 0.

Sequin answered 7/2, 2019 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.