Here is described the nullopt_t
and nullopt
for the optional
object proposed for c++:
struct nullopt_t{see below}; constexpr nullopt_t nullopt(unspecified);
[...] Type nullopt_t shall not have a default constructor. It shall be a literal type. Constant nullopt shall be initialized with an argument of literal type.
The reason for this is explained in the The op = {} syntax chapter of the document: for the op = {}
to be unambiguous some tricks have to be adopted, one of which is that nullopt_t
must not be default constructible.
My question is about what does the literal type means here? I found this SO post. So it looks to me that just another empty class would do. Could it also be a constructor taking a int
?
What would be a minimal conforming nullopt_t
class look like?
Something like this:
struct nullopt_t_construct_tag_t{};
struct nullopt_t {
nullopt_t() = delete; // I know declaring it as deleted is redundant
constexpr nullopt_t(nullopt_t_construct_tag_t) {};
};
constexpr nullopt_t nullopt(nullopt_t_construct_tag_t{});
Or this:
struct nullopt_t {
nullopt_t() = delete;
constexpr nullopt_t(int) {};
};
constexpr nullopt_t nullopt(0);