Experimental :: optional constructor nullopt_t

This describes nullopt_t and nullopt for the optional object proposed for C ++:

 struct nullopt_t{see below}; constexpr nullopt_t nullopt(unspecified); 

[...] The nullopt_t type should not have a default constructor. It must be a literal type. The nullopt constant is initialized with an argument of the literal type.

The reason for this is explained in the op = {} chapter of the document syntax : for op = {} be unambiguous, some tricks must be accepted, one of which is that nullopt_t does not have to be constructive by default.

My question is, what does the literal type mean here? I found this SO post . So it seems to me that this will make another empty class. Could it be a constructor with int ?

What does the minimally matching nullopt_t class nullopt_t ?

Something like that:

 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 that:

 struct nullopt_t { nullopt_t() = delete; constexpr nullopt_t(int) {}; }; constexpr nullopt_t nullopt(0); 
+6
source share
1 answer

Minimal implementation

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

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

If the constructor of class X not declared by the user, the constructor without parameters is implicitly declared as default (8.4).

... and nullopt_t can be built from int , a literal type.
Note that there is a default constructor in your code, although it is defined as remote.

The above definition meets the requirements for a literal like:

A type is a literal type, if any:
(10.5) - the type of class (clause 9) which has all of the following properties:

  • he has a trivial destructor,
  • is it an aggregate type (8.5.1) or has at least one constexpr constructor [..], which is not a copy or move constructor, but
  • all of its non-static data elements and base classes are non-volatile literals.
+5
source

Source: https://habr.com/ru/post/982002/


All Articles