This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Idea for more delegations in <algorithm>


I've had a look at enable_if, and I think we can make use of it still. Unless someone says what is below is pointless / broken, I'll make a clean demo patch sometime tonight / tomorrow.

First a little background. If you have a function where the return type is enable_if<a,b> then if a::value=true then the return type is simply b. If however a::value=false, then due to magic of templates this function is simply ignored by the compiler! So if you have a bunch of identical functions except for the return value (which are all enable_ifed), then as long as at most one is ever true, you get dispatch!
So consider a template


typename<typename It, typename tag, typename tag>
  struct Between
  { static const bool value = ?;};

Where value is true if It's tag type is between the two given
iterator_tags (inclusive).

Then do

template<typename It, typename val>
enable_if<Between<It, input_iterator_tag, forward_iterator_tag>, It>
__find(It,It,val) {..}

typename<typename It, typename val>
enable_if<Between<It, bidirectional_iterator_tag,
                     random_access_iterator_tag, It>
__find(It,It,val) {..}

typename<typename It, typename val>
It find(It,It,val)
{ return __find(..); }

Of course, we could just use classes, but this seems much shorter :) The
only tiny problem is you have to remember to make sure you cover all the
cases with your "Betweens" (that the algorithm accepts) and you don't
cover any twice.. not too hard however!

Chris


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]