why aren't comparators passed as refs or c-refs in algorithms?

  • Follow


Hello
if you take for example:

template<class ForwardIterator, class Compare>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
Compare comp);

I am curious why hasn't been chosen to be passed as:

template<class ForwardIterator, class Compare>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
const Compare& comp);

or a version without const?

I guess this is more restrictive, but I have in mind the cost of passing the
comparator by value instead of by reference.

I am writing a somehow generic member function template that returns the max
element of a container based on the comparator, should I just do like STL?

Regards,


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply hicham (107) 7/19/2010 9:44:32 PM

On 20 Jul., 06:44, "Hicham Mouline" <hic...@mouline.org> wrote:
> Hello
> if you take for example:
>
> template<class ForwardIterator, class Compare>
> ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
> Compare comp);
>
> I am curious why hasn't been chosen to be passed as:
>
> template<class ForwardIterator, class Compare>
> ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
> const Compare& comp);
>
> or a version without const?

This was an intentional design. In general, a call-by-value approach
has the effect that it allows better optimizations because it reduces
the chance of indirect calls. Vandevoorde/Josuttis describe that in
more detail in "C++ Templates: The Complete Guide" (sorry, I have
the book not on my desk and cannot quote the precise section).

> I guess this is more restrictive, but I have in mind the cost of passing
the
> comparator by value instead of by reference.

Typically there is no additional cost, because many function object
types are empty class types. If they are not, you can simply
change that e.g. by providing boost::reference_wrapper or
std::reference_wrapper (as of C++0x) instead.

> I am writing a somehow generic member function template that returns the
max
> element of a container based on the comparator, should I just do like STL?

I strongly suggest to follow this approach unless it is very
important that the caller object should keep state. A notable
exception from the general rule is the algorithm std::random_shuffle
with a third parameter (and the new algorithm std::shuffle in
C++0x).

HTH & Greetings from Bremen,

Daniel Kr�gler



-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply ISO 7/20/2010 7:02:52 AM


On 7/19/2010 9:44 PM, Hicham Mouline wrote:
> Hello
> if you take for example:
>
> template<class ForwardIterator, class Compare>
> ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
> Compare comp);
>
> I am curious why hasn't been chosen to be passed as:
>
> template<class ForwardIterator, class Compare>
> ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
> const Compare&  comp);
>
> or a version without const?
>
> I guess this is more restrictive, but I have in mind the cost of passing the
> comparator by value instead of by reference.
>

In addition to why Daniel said... think about this:

If you passed by const-ref, and the functor had changeable state,
said member variables would need to be declared mutable.

Similarly, if you passed by ref instead of value or const-ref, you
wouldn't be able to pass a temporary object as the functor.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply red 7/21/2010 6:04:28 AM

Hicham Mouline wrote:

> Hello
> if you take for example:
>
> template<class ForwardIterator, class Compare>
> ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
> Compare comp);
>
> I am curious why hasn't been chosen to be passed as:
>
> template<class ForwardIterator, class Compare>
> ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
> const Compare& comp);
>
> or a version without const?
>
> I guess this is more restrictive, but I have in mind the cost of passing
> the comparator by value instead of by reference.
>
> I am writing a somehow generic member function template that returns the
> max element of a container based on the comparator, should I just do like
> STL?
>

In addition to all the good points risen by others, please notice that in
C++03 it was not valid to denote a function using a const reference 
(because
that would imply a cv-qualified function type, which was ill-formed).

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Johannes 7/21/2010 5:48:07 PM

On 07/21/2010 03:04 PM, red floyd wrote:
[snip]
>
> In addition to why Daniel said... think about this:
>
> If you passed by const-ref, and the functor had changeable state,
> said member variables would need to be declared mutable.

But does that state even have a meaning if the change is done
on a local copy? My bet is that if I indeed needed a state,
I would require it to live longer than one function call.

--
Dragan

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Dragan 7/25/2010 8:18:27 AM

On 7/25/2010 8:18 AM, Dragan Milenkovic wrote:
> On 07/21/2010 03:04 PM, red floyd wrote:
> [snip]
>>
>> In addition to why Daniel said... think about this:
>>
>> If you passed by const-ref, and the functor had changeable state,
>> said member variables would need to be declared mutable.
>
> But does that state even have a meaning if the change is done
> on a local copy? My bet is that if I indeed needed a state,
> I would require it to live longer than one function call.
>

Contrived example: you want to sum every other member of
a container.  You want to use accumulate:

struct sum_odd_members :
    public std::binary_function<int, int, int> {
    bool sum_this_element;
    int operator()(int x, int y)
    {
	if (sum_this_element)
              x += y;
         sum_this_element = !sum_this_element;
         return x;
    }
    sum_odd_members() : sum_this_element(true) { }
};

Container<int> c;
int sum_of_every_other_member =
     std::accumulate(c.begin(), c.end(), 0, sum_odd_members());

If functors were passed by const ref, this sort of (contrived)
example would be impossible, unless sum_this_element was mutable.




-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply red 7/29/2010 6:15:28 AM

5 Replies
117 Views

(page loaded in 0.076 seconds)


Reply: