gpt4 book ai didi

c++ - 显式构造函数和嵌套初始化列表

转载 作者:可可西里 更新时间:2023-11-01 16:11:05 25 4
gpt4 key购买 nike

下面的代码可以用大多数现代 C++11 兼容编译器(GCC >= 5.x、Clang、ICC、MSVC)成功编译。

#include <string>

struct A
{
explicit A(const char *) {}
A(std::string) {}
};

struct B
{
B(A) {}
B(B &) = delete;
};

int main( void )
{
B b1({{{"test"}}});
}

但是为什么首先要编译它,列出的编译器如何解释该代码?

为什么 MSVC 能够在没有 B(B &) = delete; 的情况下编译这个,但是其他3个编译器都需要吗?

以及为什么当我删除 时它在除 MSVC 之外的所有编译器中都失败不同的签名复制构造函数,例如 B(const B &) = delete; ?

编译器甚至都选择相同的构造函数吗?

为什么 Clang 会发出以下警告?
17 : <source>:17:16: warning: braces around scalar initializer [-Wbraced-scalar-init]
B b1({{{"test"}}});

最佳答案

我将尝试解释标准所说的内容,而不是解释编译器的行为。

主要示例

直接初始化 b1来自 {{{"test"}}} ,重载决议适用于选择B的最佳构造函数.因为没有来自 {{{"test"}}} 的隐式转换至 B& (列表初始值设定项不是左值),构造函数 B(B&)不可行。然后我们关注构造函数 B(A) ,并检查它是否可行。

{{{"test"}}} 确定隐式转换序列至 A (为了简单起见,我将使用符号 {{{"test"}}} -> A),重载决议适用于选择 A 的最佳构造函数,所以我们需要比较 {{"test"}} -> const char*{{"test"}} -> std::string (注意最外层的括号被省略)根据 [over.match.list]/1 :

When objects of non-aggregate class type T are list-initialized such that [dcl.init.list] specifies that overload resolution is performed according to the rules in this subclause, overload resolution selects the constructor in two phases:

  • Initially, the candidate functions are the initializer-list constructors ([dcl.init.list]) of the class T...

  • If no viable initializer-list constructor is found, overload resolution is performed again, where the candidate functions are all the constructors of the class T and the argument list consists of the elements of the initializer list.

... In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed.



请注意,无论说明符 explicit 是什么,这里都会考虑所有构造函数。 .
{{"test"}} -> const char*根据 [over.ics.list]/10 不存在和 [over.ics.list]/11 :

Otherwise, if the parameter type is not a class:

  • if the initializer list has one element that is not itself an initializer list...

  • if the initializer list has no elements...

In all cases other than those enumerated above, no conversion is possible.



确定 {{"test"}} -> std::string ,同样的过程,重载决议选择 std::string的构造函数接受类型为 const char* 的参数.

结果, {{{"test"}}} -> A通过选择构造函数来完成 A(std::string) .

变化

如果 explicit怎么办已移除?

过程不会改变。 GCC 会选择构造函数 A(const char*)而 Clang 将选择构造函数 A(std::string) .我认为这是 GCC 的一个错误。

如果 b1的初始化器中只有两层大括号怎么办? ?

备注 {{"test"}} -> const char*不存在但 {"test"} -> const char*存在。所以如果 b1的初始化器中只有两层大括号, 构造函数 A(const char*)被选中是因为 {"test"} -> const char*优于 {"test"} -> std::string .结果,在复制列表初始化中选择了显式构造函数(构造函数 A 中的参数 B(A) 来自 {"test"} 的初始化),然后程序是非良构的。

如果构造函数 B(const B&) 呢?被宣布?

请注意,如果声明 B(B&),也会发生这种情况。已移除。这次我们需要比较 {{{"test"}}} -> A{{{"test"}}} -> const B& , 或 {{{"test"}}} -> const B相当于。

确定 {{{"test"}}} -> const B ,采用上述过程。我们需要比较 {{"test"}} -> A{{"test"}} -> const B& .备注 {{"test"}} -> const B&根据 [over.best.ics]/4 不存在:

However, if the target is

— the first parameter of a constructor or

— the implicit object parameter of a user-defined conversion function

and the constructor or user-defined conversion function is a candidate by

— [over.match.ctor], when the argument is the temporary in the second step of a class copy-initialization,

— [over.match.copy], [over.match.conv], or [over.match.ref] (in all cases), or

the second phase of [over.match.list] when the initializer list has exactly one element that is itself an initializer list, and the target is the first parameter of a constructor of class X, and the conversion is to X or reference to cv X,

user-defined conversion sequences are not considered.



确定 {{"test"}} -> A ,再次进行上述过程。这与我们在上一小节中讨论的案例几乎相同。结果,构造函数 A(const char*)被选中。注意这里选择构造函数来确定 {{{"test"}}} -> const B ,实际上并不适用。尽管构造函数是显式的,但这是允许的。

结果, {{{"test"}}} -> const B通过选择构造函数来完成 B(A) ,然后是构造函数 A(const char*) .现在都 {{{"test"}}} -> A{{{"test"}}} -> const B是用户定义的转换序列,两者都不优于另一个,所以 b1的初始化是模棱两可的。

如果括号被大括号代替怎么办?

根据 [over.best.ics]/4 ,在上一小节中被块引用,用户定义的转换 {{{"test"}}} -> const B&不考虑。因此即使构造函数 B(const B&) 结果与主要示例相同被宣布。

关于c++ - 显式构造函数和嵌套初始化列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534938/

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