繁体   English   中英

为什么采用迭代器的构造函数需要元素为EmplaceConstructible?

[英]Why does a constructor which takes iterators require elements to be EmplaceConstructible?

我已经在标准(n4296),23.2.3 / 4(表100)中看到了对序列stl容器的要求,并且读过一个带有参数迭代器的构造函数(X - 容器,i和j - 输入迭代器)

X(i, j)
X a(i, j)

要求容器的元素类型为EmplaceConstructible。

Requires: T shall be EmplaceConstructible into X from *i

我认为可以通过为范围中的每个迭代器调用std :: allocator_traits :: construct(m,p,* it)方法来实现构造函数(其中m - 类型为A的分配器,p - 指向内存的指针,它 - 迭代器in [i; j),并且只需要CopyInsertable元素的概念,因为只提供一个参数用于复制/移动,而EmplaceConstructible概念要求元素由一组参数构造。 这个决定有什么理由吗?

CopyInsertable是一个二进制概念 - 给定一个容器X它适用于单个类型T ,它需要有一个复制构造函数。 但是, *i被允许与T不同,只要有一种方法(隐式)从*i构造T

  char s[] = "hello world!";
  std::vector<int> v(std::begin(s), std::end(s));
  // int is EmplaceConstructible from char

一个(人为的)示例,其中T 不是 CopyInsertable

struct nocopy {
  nocopy(int) {}
  nocopy(nocopy const&) = delete;
  nocopy(nocopy&&) = delete;
};
int a[]{1, 2, 3};
std::vector<nocopy> v(std::begin(a), std::end(a));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM