2015-12-16 69 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'是直接初始化的,並且會很樂意爲你做明確的轉換,無論好壞。 –

相關問題