How to implement the fill constructor and range constructor for sequence containers explicitly

Sequence containers must have fill constructors and range constructors, i.e. they should work, suggesting that MyContainersimulates container sequence value_typewhich intand size_typeequals std::size_t:

// (1) Constructs a MyContainer containing the number '42' 4 times.
MyContainer<int> c = MyContainer<int>(4, 42);

// (2) Constructs a MyContainer containing the elements in the range (array.begin(), array.end())
std::array<int, 4> array = {1, 2, 3, 4};
MyContainer<int> c2 = MyContainer<int>(array.begin(), array.end());

The problem is that I'm not sure how to implement these two constructors. These signatures do not work:

template<typename T>
MyContainer<T>::MyContainer(const MyContainer::size_type n, const MyContainer::value_type& val);

template<typename T>
template<typename OtherIterator>
MyContainer<T>::MyContainer(OtherIterator i, OtherIterator j);

In this case, the instance, as in example 1 above, selects the range constructor instead of the fill constructor, since it 4is an int, not a size_type. It works if I pass 4u, but if I understand the requirements correctly, any positive integer should work.

, , , value_type .

Visual ++ std::vector, , , (_Is_iterator<_Iter>). ++.

, ... ?

. ++ 11, boost .

0
1

, : , size_t -typed n s, SFINAE, . , , MSVC _Is_iterator "" (.. , ). , , . , , ++.

, , , .

+1

Source: https://habr.com/ru/post/1684503/


All Articles