2015-12-16 67 views
3

我已经看到标准(n4296),23.2.3/4(表100)中的序列stl容器的要求,并且读取了一个构造函数,它带有参数 - 迭代器(X - 容器,i和j - 输入迭代器)为什么需要迭代器的构造函数需要元素为EmplaceConstructible?

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

要求容器的元素类型为EmplaceConstructible。

Requires: T shall be EmplaceConstructible into X from *i 

我认为一个构造可以通过调用的std :: allocator_traits ::构建体(M,P,*它)方法中的范围(实施针对每个迭代器,其中m - A型分配器,对 - 指针在内存中,it - iterator在[i; j)中,并且只需要CopyInsertable概念,因为只有一个参数用于复制/移动,而EmplaceConstructible概念需要使用一组参数构造元素。这个决定有什么理由吗?

回答

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 

A(人为)示例,其中TCopyInsertable

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)); 
+0

s/implicit/explicit /。 'allocator_traits :: construct'是直接初始化的,并且会很乐意为你做明确的转换,无论好坏。 –

相关问题