为什么一个使用迭代器的构造函数要求元素是EmplaceConstructable
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)
要求容器的元素类型为EmplaceConstructable。
Requires: T shall be EmplaceConstructible into X from *i
我认为构造函数可以通过为范围内的每个迭代器调用std::allocater_traits::construct(m,p,*it)方法来实现(其中m-类型为a的分配器,p-指向内存的指针,it-[I;j中的迭代器),并且元素只需要CopyInsertable概念,因为只提供了一个参数用于复制/移动,而EmplaceConstructable概念要求元素从一组参数中构造。做出这个决定有什么理由吗?
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));
相关文章:
- 复制包含C++所有元素的对象!(构造函数和赋值,最佳实践?
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 如何在 c++ 中将整个数组初始化为类构造函数中的单个元素
- 无法在构造函数中执行设置元素插入
- 如何在构造函数初始值设定项列表中使用 n 个元素初始化 std::vector<std::time_t>
- 如何在类中制作 2D 数组元素,然后在其构造函数中指定其维度?
- std::tuple默认构造函数,带有move可构造元素
- 如果我想从类型"T"定义元素的容器(来自 STL),那么"T"必须使用默认构造函数?
- 为 unordered_map 中的元素设置默认构造函数(如果是 [] 运算符)
- 在 new 关键字中,由默认构造函数初始化的类中的元素是否也使用 new 关键字在C++?
- 为什么只有在向量中已经有一个元素时才调用移动构造函数?
- 我们如何使用下面的集合构造函数构造设置元素
- clang格式:如何将构造函数的初始值设定项列表的每个元素保存在单独的行上
- 在不调用构造函数的情况下创建 Vector 元素
- C2280 STD :: vector的默认构造函数尽管具有移动构造函数,但仍需要元素的复制构造函数
- 实例化 std::vector 的唯一元素,而无需复制构造函数
- 覆盖数组元素时如何调用构造函数
- 在分配器感知类调用复制构造函数中对向量元素的引用
- 将元素添加到矢量时复制构造函数
- 无法使用迭代器擦除矢量元素,矢量元素实现移动构造函数