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

Quick Newbie question

49 views
Skip to first unread message

Doug Mika

unread,
Apr 27, 2015, 3:55:26 PM4/27/15
to
I am learning to properly read the www.cplusplus.com reference material to quickly decipher what it says, and I have come upon the following:

the std::list class has a sort member function that comes in two overloaded forms. The first is:

void sort();

which easily enough says that for any instance of a list you can invoke the sort() member function.

The problem I have, is how do I read the second overloaded form of sort:

template <class Compare>
void sort (Compare comp);

I know the basics of templates, in particular that these can be used to define template functions and template classes, but I have never heard of class member functions themselves being template member functions. Basically, looking at this second overloaded form of sort, how would I know that this sort takes a predicate or a function (ie how should I know that Compare is a function or I guess a functor?)

Thanks

Paavo Helde

unread,
Apr 27, 2015, 4:13:53 PM4/27/15
to
Doug Mika <doug...@gmail.com> wrote in
news:f1da6908-d1f0-41b1...@googlegroups.com:

> The problem I have, is how do I read the second overloaded form of
> sort:
>
> template <class Compare>
> void sort (Compare comp);
>
> I know the basics of templates, in particular that these can be used
> to define template functions and template classes, but I have never
> heard of class member functions themselves being template member
> functions. Basically, looking at this second overloaded form of sort,
> how would I know that this sort takes a predicate or a function (ie
> how should I know that Compare is a function or I guess a functor?)

By reading the documentation. On the
http://www.cplusplus.com/reference/list/list/sort/?kw=list%3A%3Asort page
it says for example:

comp: Binary predicate [...] This shall be a function pointer or a
function object.

And there is also a relevant example included.

There is a long-developed C++ feature ("concepts") which would formalize
such requirements, but doing this properly has proved to be tricky and
this feature is not yet standardized.

hth
Paavo

Doug Mika

unread,
Apr 27, 2015, 4:28:17 PM4/27/15
to
ok, but why does a class member function have

template<class Compare>

in front of it? What does this represent? What does it indicate in the instance of std::list.sort(comp)?

Barry Schwarz

unread,
Apr 27, 2015, 4:34:48 PM4/27/15
to
Didn't the example further down the web page make it clear.?

To be consistent, you read the first form as "sort the elements of the
list using the default operator< that may be defined for the element
type as the comparison function." If the list contains strings, then
it would use the operator< defined for the string class. If the list
contains int, then it would use the < operator defined for int. If
the list contains struct, it should generate a diagnostic that there
is not default operator< defined for struct.

Read the second as "sort the elements of the list using the specified
function as the comparison function." It is your job to insure the
specified function exists, takes the correct argument types, and
returns either 0 or 1 depending on the result of the comparison.

--
Remove del for email

Paavo Helde

unread,
Apr 28, 2015, 12:20:57 AM4/28/15
to
Doug Mika <doug...@gmail.com> wrote in
news:cdda1042-560d-46a6...@googlegroups.com:

> ok, but why does a class member function have
>
> template<class Compare>
>
> in front of it? What does this represent? What does it indicate in
> the instance of std::list.sort(comp)?

Why, you already said yourself it is a member function template:

> I have never heard
> of class member functions themselves being template member functions.

Yes, the class which it is a member of is a class template itself, so
what? And as with all template parameters of the 'class' or 'typename'
type, it means it needs to be instantiated with some suitable type.

std::list<A> myList; // Class instantiated with type A.
B b;
myList.sort<B>(b); // Member fn instantiated with type B.

(Here, <B> is usually omitted in real code as the compiler is typically
able to deduce it easily from the type of passed argument b, but one can
specify it explicitly as well).

hth
Paavo

Jorgen Grahn

unread,
Apr 28, 2015, 6:25:53 PM4/28/15
to
On Tue, 2015-04-28, Paavo Helde wrote:
> Doug Mika <doug...@gmail.com> wrote in
> news:cdda1042-560d-46a6...@googlegroups.com:
>
>> ok, but why does a class member function have
>>
>> template<class Compare>
>>
>> in front of it? What does this represent? What does it indicate in
>> the instance of std::list.sort(comp)?
>
> Why, you already said yourself it is a member function template:
>
>> I have never heard
>> of class member functions themselves being template member functions.
>
> Yes, the class which it is a member of is a class template itself, so
> what?

And if your head starts to explode thinking of it, just pick a
concrete example like std::list<int> and think of what its sort()
functions would mean and what Compare could be.

That's a "trick" I often use.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages