C++11 标准是否要求实现者优先考虑 std::vector 的 noexcept 移动构造函数而不是 const 复制构造函数?

2024-04-02

Reading this https://stackoverflow.com/questions/28627348 and this https://stackoverflow.com/questions/26224112 and 23.3.6.5/1 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf标准的最新 C++ 标准草案中规定实现者应优先使用非抛出移动构造函数T(T &&t) noexcept通过 const 复制构造函数T(const T &t) when std::vector<T>由于 a 的结果而重新分配其元素push_back手术?是吗13.3.3.1.4/1 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf关于引用绑定的重载解析?

EDIT 1

我争论的是13.3.3.1.4/1 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf由于以下原因:

  • 13.3/2 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf

    Overload resolution selects the function to call in seven distinct contexts within the language. [...] invocation of a constructor for direct-initialization (8.5) of a class object (13.3.1.3); [...] Each of these contexts defines the set of candidate functions and the list of arguments in its own unique way. But, once the candidate functions and argument lists have been identified, the selection of the best function is the same in all cases: First, a subset of the candidate functions (those that have the proper number of arguments and meet certain other conditions) is selected to form a set of viable functions (13.3.2). Then the best viable function is selected based on the implicit conversion sequences (13.3.3.1) needed to match each argument to the corresponding parameter of each viable function.

  • 13.3.2/1 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf

    From the set of candidate functions constructed for a given context (13.3.1), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences for the best fit (13.3.3). The selection of viable functions considers relationships between arguments and function parameters other than the ranking of conversion sequences.

  • 13.3.1.3/1 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf

    When objects of class type are direct-initialized (8.5), or copy-initialized from an expression of the same or a derived class type (8.5), overload resolution selects the constructor.

  • 13.3.3.1/5 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf

    For the case where the parameter type is a reference, see 13.3.3.1.4.

  • 13.3.3.1.4/1 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf

    When a parameter of reference type binds directly (8.5.3) to an argument expression, the implicit conversion sequence is the identity conversion, [...]

因此,我得出的结论是,身份转换的要求导致需要优先考虑T(T &&t) noexcept over T(const T &t)。但是,与我争论的对方并不相信。所以,我在这里问一下。

EDIT 2

以下是之间的链接23.3.6.5 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf and 13.3.3.1.4 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf:

First, 23.3.6.5 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf需要以下的std::vector:

[...] void push_back(const T& x); void push_back(T&& x);备注:[...]如果抛出异常以外通过 T 的复制构造函数、移动构造函数、赋值运算符或移动赋值运算符或通过任何 InputIterator 操作,有no影响。 [...]

涵盖的是23.3.6.1/2 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf这需要以下条件:

A vector满足all容器和可翻转容器的要求(在 23.2 的两个表中给出),[...]

那是,std::vector预计符合23.2.1/7 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf指定以下内容:

除非另有规定,all本节中定义的容器获取内存使用分配器(见 17.6.3.5)。

23.2.1/3 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf指定以下内容:

对于受本子条款影响的组件,声明allocator_type,存储在这些组件中的对象应使用allocator_traits<allocator_type>::construct功能并使用 allocator_traits::destroy 函数销毁(20.7.8.2)。这些函数仅针对容器的元素类型调用,而不针对容器使用的内部类型。

Since 20.7.8.2 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf仅指定一个construct函数如下:

template <class T, class... Args>
static void construct(Alloc& a, T* p, Args&&... args);

with 20.7.8.2/5 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf要求如下:

效果:通话a.construct(p, std::forward<Args>(args)...)该调用是否格式正确;否则,调用::new (static_cast<void*>(p)) T(std::forward<Args>(args)...).

and 20.7.9.2/12 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf需要以下默认分配器:

效果:::new((void *)p) U(std::forward<Args>(args)...)

, both T(std::forward<Args>(args)...) in 20.7.8.2/5 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf and U(std::forward<Args>(args)...) in 20.7.9.2/12 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf将遵循构造函数重载解析要求13.3.3.1.4/1 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf.

因此,该标准要求实施者优先考虑使用 move-constructorT(T &&t) noexcept过度复制构造函数T(const T &t) when std::vector<T>由于重新分配其元素push_back运营。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++11 标准是否要求实现者优先考虑 std::vector 的 noexcept 移动构造函数而不是 const 复制构造函数? 的相关文章

随机推荐