From: Hicham Mouline on
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! ]

From: Daniel Krügler on
On 20 Jul., 06:44, "Hicham Mouline" <hic...(a)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! ]

From: red floyd on
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! ]

From: Johannes Schaub (litb) on
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! ]

From: Dragan Milenkovic on
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! ]