gpt4 book ai didi

c++ - 被删除的构造函数 "accessible"吗?

转载 作者:IT老高 更新时间:2023-10-28 22:19:54 24 4
gpt4 key购买 nike

this question 上的已删除答案关于已删除的移动构造函数引号 cppreference.com as saying is_move_constructible 特征应该成功,只要移动构造函数是“可访问的”,即使它不是“可用的”。

该标准实际上要求参数类型的移动构造是格式良好的,因此答案并不完全正确。

现在,该标准反复使用术语“可访问”来指代实际可构造性的构造函数。例如:

[C++11 8.5/6]: To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.

If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

但是,我在标准中找不到任何地方明确说明 deleted、显式定义的构造函数是否“可访问”。

不同的 [非规范] 引用似乎表明 deleted-ness 和可访问性是正交的:

[C++11: 12.2/1]: [..] [ Note: even if there is no call to the destructor or copy/move constructor, all the semantic restrictions, such as accessibility (Clause 11) and whether the function is deleted (8.4.3), shall be satisfied. [..]

  • 我错过了一段吗?
  • 如果没有,是否应该更正 cppreference.com 页面?您能提出更好的措辞吗?
  • 无论哪种方式,标准都应该更明确吗?

最佳答案

我不想解决 cppreference 网站所说的内容,但就标准而言,可构造性不是根据“可访问构造函数”定义的。相反,主要定义是is_constructible,即(C++11, 20.9.4.3/6):

is_constructible<T, Args...>

shall be satisfied if and only if the following variable definition would be well-formed for some invented variable t:

T t(create<Args>()...);

Access checking is performed as if in a context unrelated to T and any of the Args. Only the validity of the immediate context of the variable initialization is considered.

因此,最后一行代码的假设表达式的格式良好是可构造性特征的定义特征。这与使用已删除函数会导致程序格式错误的子句密切相关。

关于c++ - 被删除的构造函数 "accessible"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20816117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com