rize
rize

Reputation: 859

Using sorting predicates in C++

I have implemented a comparison for struct Track in the function:

bool sortingPredicate(const Track& l, const Track& r)

then I have a function:

void sortPlaylist(std::list<Track>& playlist, bool (*predicate)(const Track& l, const Track& r)) {
    playlist.sort(predicate);
}

And I have tried:

std::list<Track> mergeWithoutDuplicates(const std::list<Track>& l, const std::list<Track>& r) {
    sortPlaylist(l, sortingPredicate<Track>());
    ...
}

And I get for the sortPlaylist-call:

error: expected primary-expression before ‘>’ token
error: expected primary-expression before ‘)’ token"

What am I missing in the function call / doing wrong? Many thanks.

Upvotes: 0

Views: 244

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258618

sortingPredicate<Track>()

is a function call, you're not passing it as a callback. It should be:

sortPlaylist(l, sortingPredicate);

also, note that l is const inside mergeWithoutDuplicates, but you're passing it to sortPlaylist which expects a non-const reference. That's also wrong.

Upvotes: 1

Related Questions