简体   繁体   English

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

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

I have seen requirements for a sequence stl containers in the Standard (n4296), 23.2.3/4 (Table 100) and have read that a constructor which takes arguments-iterators (X - container, i and j - input iterators) 我已经在标准(n4296),23.2.3 / 4(表100)中看到了对序列stl容器的要求,并且读过一个带有参数迭代器的构造函数(X - 容器,i和j - 输入迭代器)

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

requires a container's element type to be EmplaceConstructible. 要求容器的元素类型为EmplaceConstructible。

Requires: T shall be EmplaceConstructible into X from *i

I think that a constructor can be implemented through calling std::allocator_traits::construct (m, p, *it) method for each iterator in range (where m - allocator of type A, p - pointer to memory, it - iterator in [i; j), and there is only CopyInsertable concept for elements is required because only one argument is provided for copying/moving, whereas EmplaceConstructible concept requires element to be constructed from a set of arguments. 我认为可以通过为范围中的每个迭代器调用std :: allocator_traits :: construct(m,p,* it)方法来实现构造函数(其中m - 类型为A的分配器,p - 指向内存的指针,它 - 迭代器in [i; j),并且只需要CopyInsertable元素的概念,因为只提供一个参数用于复制/移动,而EmplaceConstructible概念要求元素由一组参数构造。 Is there any reasons for that decision? 这个决定有什么理由吗?

CopyInsertable is a binary concept - given a container X it applies to a single type T , which is required to have a copy constructor. CopyInsertable是一个二进制概念 - 给定一个容器X它适用于单个类型T ,它需要有一个复制构造函数。 However, *i is allowed to be a different type from T , as long as there is a way to (implicitly) construct T from *i : 但是, *i被允许与T不同,只要有一种方法(隐式)从*i构造T

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

A (contrived) example where T is not CopyInsertable : 一个(人为的)示例,其中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.

相关问题 为什么std :: reverse需要双向迭代器? - Why does std::reverse require bidirectional iterators? 为什么并行for_each需要前向迭代器? - Why does the parallel for_each require forward iterators? 为什么noexcept构造函数需要实例化析构函数? - Why does a noexcept constructor require instantiation of the destructor? 为什么移动构造函数需要其成员的默认构造函数? - Why does a move constructor require a default constructor for its members? 比较两个map :: iterators:为什么需要std :: pair的拷贝构造函数? - Comparing two map::iterators: why does it need the copy constructor of std::pair? 为什么 std::array 需要大小作为模板参数而不是构造函数参数? - Why does std::array require the size as a template parameter and not as a constructor parameter? 为什么 std::array 没有一个构造函数来为要填充的数组取值? - Why does std::array not have an constructor that takes a value for the array to be filled with? 为什么IMPLEMENT_DYNAMIC需要一个空的构造函数? - Why does IMPLEMENT_DYNAMIC require an empty constructor? 为什么我的Arduino类构造函数需要参数? - Why does my Arduino Class Constructor require an argument? 为什么range-v3 yield需要默认构造函数 - why does range-v3 yield require default constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM