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