Reducing overload set using enable_if

Started by
2 comments, last by the_edd 12 years, 2 months ago
I've been working on a ranged-based STL like Alexandrescu proposed in his "Iterator's Must Go" talk. I've encountered an issue with constructing a container from a range, and I've been trying to use enable_if to fix it. I have it working, but looking for an explanation why my other attempts failed, and see if there's any better ideas out there.

The vector class has these two constructors (among others):

explicit vector(size_type n);
template <class InputIterator>
vector(InputIterator first, InputIterator last, const Allocator& = Allocator());


Naturally I can construct a vector like this, and the int->size_t promotion calls the first constructor:

std::vector v(5);

No problems yet. The problem is if you replace the iterator version with a range version, you get something like this (my class is called Array):

explicit Array(size_t n, const Allocator& = Allocator());
template <typename InputRange>
explicit Array(InputRange r, const Allocator& = Allocator());


The difference is they now have the same number of arguments. So when you call this:

Array<int> a(5);

You have two possibilities:

1) Perform int->size_t promotion and call the first constructor
2) Call range version using InputRange=int

And the second one is chosen, thus failing to compile because the expected range methods (Front, PopFront, Empty) can't be called on an int. This is a trivial problem to work around (cast to size_t, add int overload, etc.), I feel there has to be a better way. I've been trying to find a way to reduce the overload set to not include the range version on integral types, but I can't seem to get it working in all cases. Here was my first attempt:

template <typename InputRange>
explicit Array(typename std::enable_if<!std::is_integral<InputRange>::value, InputRange>::type r, const Allocator& = Allocator());


This correctly forces an int to call the size_t constructor, but also is trying to make a range take the size_t constructor too. I haven't really figured out why, since I've tried printing out the result of that expression and the type is correct. However, since that's pretty un-readable, I'd rather put the enable_if elsewhere, like in an extra parameter like this:

template <typename InputRange>
explicit Array(InputRange r, const Allocator& = Allocator(), typename std::enable_if<!std::is_integral<InputRange>::value, void>::type* = nullptr);


And for some reason this overload works. So can someone explain why putting the enable_if on the first parameter wasn't working? I'd also be interested in hearing if there's other ideas on how to work around this problem out there.

Thanks.
-- gekko
Advertisement
enable_if takes a meta-function but you're passing it a value. You could use enable_if_c instead, though [s](but I suspect compilation will fail as the template argument can't be instantiated -- enable_if will do it lazily)[/s]. However, what you probably want is disable_if:



#include <new>
#include <cstddef>
#include <iostream>

#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_integral.hpp>

template<typename T, typename Allocator = std::allocator<T> >
struct Array
{
explicit Array(size_t, const Allocator & = Allocator())
{
std::cout << "size_t ctor\n";
}

template<typename InputRange>
explicit Array(InputRange, const Allocator & = Allocator(), typename boost::disable_if<boost::is_integral<InputRange> >::type * = 0)
{
std::cout << "range ctor\n";
}
};

int main()
{
Array<bool> a1(4);
Array<bool> a2(4.0F);
return 0;
}
Looks like the enable_if from C++11 is enable_if_c from Boost, and there is no disable if (since you can flip the bool on enable_if). So what you have there is the Boost equivalent of what I provided.

In looking at the Boost docs to write this post, adding an extra parameter is common. Not entirely sure why I can't just change the type of the first argument, but I'll keep looking into it.
-- gekko
Ah sorry, didn't spot the "std::".

This topic is closed to new replies.

Advertisement