gpt4 book ai didi

c++ - 使用迭代器的 std::vector 模板构造函数是否允许转换?

转载 作者:IT老高 更新时间:2023-10-28 22:14:38 26 4
gpt4 key购买 nike

在 C++11 标准第 23.3.6.2 节 [vector.cons] 中,如下所述:

   template <class InputIterator>
vector(InputIterator first, InputIterator last,
const Allocator& = Allocator());

9 Effects: Constructs a vector equal to the range [first,last), using the specified allocator.
10 Complexity: Makes only N calls to the copy constructor of T (where N is the distance between first and last) and no reallocations if iterators first and last are of forward, bidirectional, or random access categories. It makes order N calls to the copy constructor of T and order log(N) reallocations if they are just input iterators.

(此文本也存在于旧标准中)。一方面,它不要求取消引用 InputIterator 应该导致存储在 vector 中的相同类型的值。另一方面,它讲述了使用复制构造函数,这意味着相同的类型。

我的问题是:如果可以在类型之间进行转换,那么在此构造函数中使用一系列不同类型的元素是否有效?引用标准是可取的。

例如,以下代码可以正常工作 at ideone .它是由标准保证的,还是只是 GCC 恰好允许它?

#include <vector>
#include <iostream>

struct A {
int n;
A(int n_) : n(n_) {}
};

int main() {
int arr[] = {1,2,3,4,5,6,7,8,9,10};
std::vector<int> int_vec(arr, arr+10);
std::vector<A> A_vec(int_vec.begin(), int_vec.end());

for( std::vector<A>::iterator it=A_vec.begin(); it!=A_vec.end(); ++it )
std::cout<< it->n <<" ";
std::cout<<std::endl;
}

最佳答案

来自 C++ 2012 年 1 月草稿:

§ 23.2.3/3 [sequence.reqmts] ....i and j denote iterators satisfying input iterator requirements and refer to elements implicitly convertible to value_type, [i, j) denotes a valid range....

X(i, j)
X a(i, j)
Requires: T shall be EmplaceConstructible into X from *i. For vector, if the iterator does not meet the forward iterator requirements (24.2.5), T shall also be MoveInsertable into X. Each iterator in the range [i,j) shall be dereferenced exactly once.
post: distance(begin(), end()) == distance(i, j) Constructs a sequence container equal to the range [i, j)

Coren让我注意到你引用的部分:

§ 23.3.6.2/8 [vector.cons] template <class InputIterator> vector(InputIterator first, InputIterator last, const Allocator& = Allocator());
Effects: Constructs a vector equal to the range [first,last), using the specified allocator.
Complexity: Makes only N calls to the copy constructor of T (where N is the distance between first and last) and no reallocations if iterators first and last are of forward, bidirectional, or random access categories. It makes order N calls to the copy constructor of T and order log(N) reallocations if they are just input iterators.

在特定于 vector 的区域中,技术上应该覆盖第一部分。但是,我认为对复制构造函数的这种引用是错误的,并且为了迂腐,复制构造函数的提及是最大的复杂性,因此对复制构造函数的 0 次调用(仅使用转换构造函数)在我看来是有效的。这没有我希望的那么清楚。

Xeo让我注意到 C++ Standard Core Language Active Issues, Revision 78有一个问题(535)是关于如何在标准中“关于复制构造的许多规定被表述为仅指“复制构造函数”。这显然是糟糕的措辞。“每次使用术语“复制构造函数” ” 应该检查标准中的内容,以确定它是否严格适用于复制构造函数或任何用于复制的构造函数。 (类似的问题适用于“复制赋值运算符”,它与赋值运算符函数模板具有相同的关系。)”因此,纠正这种糟糕的措辞是他们的待办事项。

关于c++ - 使用迭代器的 std::vector 模板构造函数是否允许转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9215391/

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