当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《为什么采用迭代器的构造函数需要元素为EmplaceConstructible?》经验,为你挑选了1个好方法。

我已经在标准(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概念要求元素由一组参数构造.这个决定有什么理由吗?



1> ecatmur..:

CopyInsertable是一个二进制概念 - 给定一个容器,X它适用于单个类型T,需要有一个复制构造函数.然而,*i允许是由不同类型的T,只要有一种方法,以(隐式地)构造T*i:

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

A(人为)示例,其中T CopyInsertable:

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

推荐阅读
mobiledu2402851373
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有