Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this constructor overload resolve incorrectly?

This is my (stripped) class and instantiation of one object:

template <typename T, typename Allocator = std::allocator<T> >
class Carray {
    typedef typename Allocator::size_type size_type;

    // ...

    explicit Carray(size_type n, const T& value, const Allocator& alloc = Allocator()) {
        // ...
    }

    template<typename InputIterator>
    Carray(InputIterator first, InputIterator last, const Allocator& alloc = Allocator()) {
        // ...
    }

    // ...
}

Carray<int> array(5, 10);

I would expect this to call the Carray(size_type, const T&, const Allocator&) constructor, but it doesn't. Apparantly this resolutes to template<typename InputIterator> Carray(InputIterator, InputIterator, const Allocator&).

What should I change to make this work as intended? I find it weird also, because a call to std::vector<int> v(5, 10) works perfectly fine. And if I look at the definition of the constructors in my GCC's implementation I find this (I renamed some compiler-implementation names, like __n):

template<typename T, typename A = std::allocator<T> >
class vector {
    typedef size_t size_type;
    typedef T value_type;
    typedef A allocator_type;

    // ...

    explicit vector(size_type n, const value_type& value = value_type(), const allocator_type& a = allocator_type());

    template<typename InputIterator>
    vector(InputIterator first, InputIterator last, const allocator_type& a = allocator_type());

    // ...
};

which seems to be the same.

like image 503
orlp Avatar asked May 18 '11 20:05

orlp


2 Answers

The explicit constructor expects a size_t and an int. You have provided two ints.

Substituting int for InputIterator makes the template a better match.

If you look closer at the standard containers, you will see that they use some template meta-programming to determine if the InputIterator could be a real iterator or if it is an integer type. This then redirects to a different construction.

Edit
Here is one way of doing it:

  template<class _InputIterator>
  vector(_InputIterator _First, _InputIterator _Last,
         const allocator_type& _Allocator = allocator_type() )
     : _MyAllocator(_Allocator), _MyBuffer(nullptr), _MySize(0), _MyCapacity(0)
  { _Construct(_First, _Last, typename std::is_integral<_InputIterator>::type()); }

private:
  template<class _IntegralT>
  void _Construct(_IntegralT _Count, _IntegralT _Value, std::true_type /* is_integral */)
  { _ConstructByCount(static_cast<size_type>(_Count), _Value); }

  template<class _IteratorT>
  void _Construct(_IteratorT _First, _IteratorT _Last, std::false_type /* !is_integral */)
  { _Construct(_First, _Last, typename std::iterator_traits<_IteratorT>::iterator_category()); }

You could also use boost::type_traits if the compiler doesn't have std::type_traits.

like image 195
Bo Persson Avatar answered Sep 28 '22 20:09

Bo Persson


Try this. It will eliminate the iterator constructor from consideration if two ints are passed:

template<typename InputIterator>
Carray(InputIterator first, InputIterator last,
    const Allocator& alloc = Allocator(),
    typename boost::disable_if<boost::is_integral<InputIterator> >::type* dummy = 0) {
}

Reference: http://www.boost.org/doc/libs/1_46_1/libs/utility/enable_if.html


EDIT: responding to "Is there any way with just the C++03 STL and without boost?"

I don't know if std::type_traits is in C++03 or not -- I always have boost available, so I just use it. But you can try this. It will work in this specific case, but may not have the generality you require:

template <class T> class NotInt { typedef void* type; };
template <> class NotInt<int> { };

template <typename T, typename Allocator = std::allocator<T> >
class Carray {
  ...
  template<typename InputIterator>
  Carray(InputIterator first, InputIterator last,
      const Allocator& alloc = Allocator(),
      typename NotInt<InputIterator>::type t = 0) {
    std::cout << __PRETTY_FUNCTION__ << "\n";
  }
};
like image 25
Robᵩ Avatar answered Sep 28 '22 18:09

Robᵩ