[英]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.