Skip to content

Latest commit

 

History

History
27 lines (18 loc) · 769 Bytes

9.12.md

File metadata and controls

27 lines (18 loc) · 769 Bytes

The constructor that takes a container to copy requires the container type and the element type of the two containers are both match exactly.

The constructor that takes two iterators requires the element type of the source container must be compatible with the element type of the destiny container.

For example,

#include <vector>
#include <list>

struct A {
};

struct B {
    B(A a) {}  // type A can covert to type B
};

int main() {
    std::vector<A> va;
    std::vector<B> vb;

    //std::list<A> la(vb.begin(), vb.end());  // Error, convert B to A
    std::list<B> lb(va.begin(), va.end());  // OK, convert A to B

    return 0;
}

The requirement of the former constructor is stricter than the latter.