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

时间:2015-12-16 11:41:53

标签: c++ c++11 stl c++14 c++-concepts

我已经在标准(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 - 指向内存的指针,它 - [i; j]中的迭代器,并且只需要CopyInsertable元素的概念,因为只提供一个参数用于复制/移动,而EmplaceConstructible概念要求元素由一组参数构造。这个决定有什么理由吗?

1 个答案:

答案 0 :(得分:7)

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

  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));
相关问题