Search code examples
c++stliteratorcontainersc++98

c++98 use iterator constructor only if InputIt is an iterator of type T


For a school project I have to implement std::vector but only using C++98 standard. The problem is that the size constructor and iterator constructor are conflicting with each other when I call it with a signed integer, so I came up with this ( whith my own implementations of enable_if, is_same, and iterator_traits):

// Size constructor
explicit vector(
    size_type       count,
    const T         &value = T(),
    const Allocator &alloc = Allocator()
) : _allocator(alloc),
    _capacity(count),
    _size(count),
    _array(_allocator.allocate(_capacity)) {
    std::fill(begin(), end(), value);
}
// Iterator constructor

template <
    class InputIt
> vector(
    InputIt first, InputIt last,
    const Allocator &alloc = Allocator(),
    typename ft::enable_if< ft::is_same< typename ft::iterator_traits< InputIt >::value_type, T >::value, int >::type = 0
) : _allocator(alloc),
    _capacity(std::distance(first, last)),
    _size(_capacity),
    _array(_allocator.allocate(_capacity)) {
    std::copy(first, last, begin());
}

But now I have a problem with my implementation of iterator_traits: when I call it with an int of course it doesn't work because int doesn't have iterator member types, but when I look at cppreference about iterator_traits, it says that If Iter does not have all five member types difference_type, value_type, pointer, reference, and iterator_category, then this template has no members by any of those names (std::iterator_traits is SFINAE-friendly) (since C++17) (until C++20) which means that the check isn't implemented before C++17, so how does the real std::vector check for Iterator validity even before C++11?

Here is the compiler error I get when calling the constructor with 2 ints:

/home/crochu/Documents/42/ft_containers/iterator_traits.hpp:22:20: error: type 'int' cannot be used prior to '::' because it has no members
                typedef typename Iter::difference_type  difference_type;
                                 ^
/home/crochu/Documents/42/ft_containers/vector.hpp:78:55: note: in instantiation of template class 'ft::iterator_traits<int>' requested here
                                typename ft::enable_if< ft::is_same< typename ft::iterator_traits< InputIt >::value_type, T >::value, int >::type = 0
                                                                                  ^
/home/crochu/Documents/42/ft_containers/main.cpp:19:20: note: while substituting deduced template arguments into function template 'vector' [with InputIt = int]
        ft::vector< int >       v(5, 42);
                                ^
In file included from /home/crochu/Documents/42/ft_containers/main.cpp:13:
In file included from /home/crochu/Documents/42/ft_containers/ft_containers.hpp:15:
/home/crochu/Documents/42/ft_containers/iterator_traits.hpp:23:20: error: type 'int' cannot be used prior to '::' because it has no members
                typedef typename Iter::value_type               value_type;
                                 ^
/home/crochu/Documents/42/ft_containers/iterator_traits.hpp:24:20: error: type 'int' cannot be used prior to '::' because it has no members
                typedef typename Iter::pointer                  pointer;
                                 ^
/home/crochu/Documents/42/ft_containers/iterator_traits.hpp:25:20: error: type 'int' cannot be used prior to '::' because it has no members
                typedef typename Iter::reference                        reference;
                                 ^
/home/crochu/Documents/42/ft_containers/iterator_traits.hpp:26:20: error: type 'int' cannot be used prior to '::' because it has no members
                typedef typename Iter::iterator_category        iterator_category;
                                 ^
5 errors generated.

Solution

  • As an example, the implementation of this constructor in libstdc++ is located in the header bits/stl_vector.h:

      template<typename _InputIterator>
        vector(_InputIterator __first, _InputIterator __last,
               const allocator_type& __a = allocator_type())
        : _Base(__a)
        {
          // Check whether it's an integral type.  If so, it's not an iterator.
          typedef typename std::__is_integer<_InputIterator>::__type _Integral;
          _M_initialize_dispatch(__first, __last, _Integral());
        }
    

    This is a tag dispatch using a proto-std::integral_constant class, to one of these functions:

      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 438. Ambiguity in the "do the right thing" clause
      template<typename _Integer>
        void
        _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
        {
          this->_M_impl._M_start = _M_allocate(_S_check_init_len(
            static_cast<size_type>(__n), _M_get_Tp_allocator()));
          this->_M_impl._M_end_of_storage =
            this->_M_impl._M_start + static_cast<size_type>(__n);
          _M_fill_initialize(static_cast<size_type>(__n), __value);
        }
    
      // Called by the range constructor to implement [23.1.1]/9
      template<typename _InputIterator>
        void
        _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
                               __false_type)
        {
          _M_range_initialize(__first, __last,
                              std::__iterator_category(__first));
        }
    

    I'd say that's about as elegant as you could get under your constraints!