本文介绍了experimental ::可选的nullopt_t构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

介绍了可选对象的code> nullopt_t 和 nullopt / p>

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);

[...]
类型nullopt_t不应该有默认构造函数。它应该是
字面类型。常量nullopt将用一个参数
初始化为字面类型。

[...] 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.

这个原因在章节:对于 op = {} 是明确的,必须采用一些技巧,其中之一是 nullopt_t 是默认构造的。

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.

我的问题是文字类型是什么意思?我发现此。所以它看起来我只是另一个空类会做。它也可以是一个构造函数取 int

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?

什么是最小符合 nullopt_t 类看起来像?

What would be a minimal conforming nullopt_t class look like?

类似这样:

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{});

或:

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

constexpr nullopt_t nullopt(0);


推荐答案

一个最小的实现是

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

没有默认构造函数将被隐式声明,[class.ctor] / 4:

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

...和 nullopt_t 可以从 int ,一个文字类型构造。

请注意,在你的代码中,默认构造函数存在,虽然被定义为已删除。

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


  • 它有一个简单的析构函数,

  • 它是一个聚合类型(8.5.1)或至少有一个 constexpr constructor [..]不是复制或移动构造函数,并且

  • 其所有非静态数据成员和基类都是非 - volatile字面类型。

  • 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.

这篇关于experimental ::可选的nullopt_t构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:19