Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sort comparator function

48 views
Skip to first unread message

Fab

unread,
Dec 14, 2013, 6:05:19 AM12/14/13
to
Dear All,

I am quite experienced with C++ but here's something I can't recall and
forgive me if I just don't recognize the (possible) simplicity of my
question.

I have the following code

// Custom comparator for pair,
// compares only by the first field
struct compare_first
{
template <typename T>
bool operator() (const T& a, const T& b)
{
return a.first < b.first;
}
};

which is later used in a sort like this

std::sort(cellid_particleid.begin(), cellid_particleid.end(), compare_first());

The sort orders a list of particles according to their cell ID. The
std::pair<unsigned int, unsigned int> contains cellID and particleID,
respectively.

Since the operator() is templated in the comparator, why does my code
still compile? I assume the struct hides the templation, but is this
taken care of in std::sort then?

Thanks + best
Fab

Alf P. Steinbach

unread,
Dec 14, 2013, 9:56:46 AM12/14/13
to
From the information given it's impossible to say what makes the code
compile.

See the FAQ item titled "How do I post a question about code that
doesn't work correctly?", currently available at (among others) <url:
http://www.parashift.com/c++-faq/posting-code.html), in particular the
three first bullet points.

It's often a good idea to take a look at the FAQ.


Cheers & hth.,

- Alf

Mr Flibble

unread,
Dec 14, 2013, 11:35:17 AM12/14/13
to
It compiles because it is correct. The member function template will be
instantiated by std::sort.

The main reason why std::less takes its argument type as a class
template parameter is because it needs to pass it on to what it derives
from (std::binary_function).

As usual safely ignore Alf as he is talking crap again.

/Flibble

Mr Flibble

unread,
Dec 14, 2013, 2:46:23 PM12/14/13
to
Obviously that is not the only reason; the ability to specify the
argument type explicitly is also sometimes needed to avoid ambiguity.

/Flibble

sg

unread,
Dec 24, 2013, 5:54:36 PM12/24/13
to
Am 14.12.2013 12:05, schrieb Fab:
>
> I have the following code
>
> struct compare_first
> {
> template <typename T>
> bool operator() (const T& a, const T& b)
> {
> return a.first < b.first;
> }
> };
>
> which is later used in a sort like this
>
> std::sort(cellid_particleid.begin(), cellid_particleid.end(), compare_first());
>
> Since the operator() is templated in the comparator, why does my code
> still compile?

Why not? -- Well, you probably should make the function call operator
const. But std::sort does not seem to mind.

> I assume the struct hides the templation, but is this
> taken care of in std::sort then?

std::sort has a named third parameter that refers to some object of your
type. Let's say, it's "comp". Then, std::sort uses that parameter like this

comp(a,b)

for some sequence elements a and b. What happens now is that the
compiler deduces the function call operator's template parameter T and
instantiates it accordingly. Done.

Anything still unclear?

Cheers!
sg
0 new messages