Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

experimental::optional nullopt_t constructor

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);
like image 519
bolov Avatar asked Feb 04 '15 21:02

bolov


1 Answers

A minimal implementation is

struct nullopt_t {
    constexpr nullopt_t(int) {}
};

No default constructor will be implicitly declared, [class.ctor]/4:

If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4).

... and nullopt_t can be constructed from int, a literal type.
Note that in your code a default constructor exists, though being defined as deleted.

The above definition does meet the requirements for a literal type:

A type is a literal type if it is:
(10.5) — a class type (Clause 9) that has all of the following properties:

  • it has a trivial destructor,
  • it is an aggregate type (8.5.1) or has at least one constexpr constructor [..] that is not a copy or move constructor, and
  • all of its non-static data members and base classes are of non-volatile literal types.
like image 166
Columbo Avatar answered Nov 06 '22 22:11

Columbo