Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing iterator value_type in range constructor

I'm writing a range cosntructor for a custom container:

MyContainer(const InputIterator& first, 
            const InputIterator& last, 
            const allocator_type& alloc = allocator_type())

and would like to check that the the InputIterator::value_type is compatible with the container's value_type. I initially tried tried:

static_assert(!(std::is_convertible<InputIterator::value_type, value_type>::value ||
        std::is_same<InputIterator::value_type, value_type>::value), "error");

and went as far as:

using InputIteratorType = typename std::decay<InputIterator>::type;
using InputValueType = typename std::decay<InputIteratorType::value_type>::type;
static_assert(!(std::is_convertible<InputValueType, value_type>::value ||
        std::is_same<InputValueType, value_type>::value), "error");

but it always asserts, even when I use the MyContainer::iterator as the input iterator.

How can I check that the InputIterator is compatible?

like image 631
Nicolas Holthaus Avatar asked Feb 19 '26 06:02

Nicolas Holthaus


1 Answers

I'm guessing you probably want std::is_constructible:

static_assert(std::is_constructible<
    value_type,
    decltype(*first)
    >::value, "error");

Or, alternatively, std::is_assignable, depending on whether you're constructing or assigning from the input iterators.

like image 62
Barry Avatar answered Feb 20 '26 19:02

Barry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!