gpt4 book ai didi

c++ - 为什么在调用不明确的ctor时没有编译时错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:03 25 4
gpt4 key购买 nike

#include <iostream>
#include <vector>

int main()
{
auto v1 = std::vector<std::size_t>(std::size_t{8});
std::cout << v1.size() << std::endl;

auto v2 = std::vector<std::size_t>{std::size_t{8}};
std::cout << v2.size() << std::endl;
}

代码输出:

8
1

我知道这是 C++ 中的一个众所周知的问题,因为:

std::vector<std::size_t>(std::size_t{8})通话

explicit vector(size_type count)同时

std::vector<std::size_t>{std::size_t{8}}通话

vector(std::initializer_list<T> init, const Allocator& alloc = Allocator()) .

令我惊讶的是:

为什么第二次调用不会触发重载解析歧义的编译时错误?

In another related question ,一段类似的代码确实会触发歧义错误。

最佳答案

因为不存在会导致错误的歧义。当我们使用列表初始化时,重载解决方案明显不同。这是有意分两个阶段完成的。

[over.match.list]

1 When objects of non-aggregate class type T are list-initialized ([dcl.init.list]), overload resolution selects the constructor in two phases:

  • Initially, the candidate functions are the initializer-list constructors ([dcl.init.list]) of the class T and the argument list consists of the initializer list as a single argument.

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

If the initializer list has no elements and T has a default constructor, the first phase is omitted. In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed. [ Note: This differs from other situations ([over.match.ctor], [over.match.copy]), where only converting constructors are considered for copy-initialization. This restriction only applies if this initialization is part of the final result of overload resolution. — end note ]

在第一步中 std::initializer_list 构造函数被考虑。我们可以到达第二步,只有在第一次重载决议失败时才考虑其他构造函数。显然,重载决议不会找不到合适的 std::initializer_list 构造函数。

您链接到的问题与初始化 vector 中的歧义无关。歧义在于选择函数重载。两者都是可行的,因为它们都接受一个不同的 vector ,该 vector 本身可以从相同的初始化列表明确初始化。

关于c++ - 为什么在调用不明确的ctor时没有编译时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57668221/

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