U
Urs Thuermann
I have written a small class Foo to test the std::sort() routine. My
class implements methods Foo(), ~Foo(), operator<(const Foo &), and
operator=(const Foo&), and a specialization of the template
std::swap(), i.e. void swap(Foo &, Foo &) and all of these print a
message that they have been called followed by theirs argument(s).
The documentation on cpluscplus.com says about swap()
"Because this function template is used as a primitive operation by
many other algorithms, it is highly recommended that large data
types overload their own specialization of this function."
so I assumed that e.g. std::sort() would only use operator<() and
swap() to do its job. But with my test class Foo I could see that
std::sort() in GCC uses many assignments instead of swap() which might
be much more expensive. Why is this done?
I have written a simple selection sort template and a quick-sort
template using only operator<() and swap() just to show that this
really works.
urs
class implements methods Foo(), ~Foo(), operator<(const Foo &), and
operator=(const Foo&), and a specialization of the template
std::swap(), i.e. void swap(Foo &, Foo &) and all of these print a
message that they have been called followed by theirs argument(s).
The documentation on cpluscplus.com says about swap()
"Because this function template is used as a primitive operation by
many other algorithms, it is highly recommended that large data
types overload their own specialization of this function."
so I assumed that e.g. std::sort() would only use operator<() and
swap() to do its job. But with my test class Foo I could see that
std::sort() in GCC uses many assignments instead of swap() which might
be much more expensive. Why is this done?
I have written a simple selection sort template and a quick-sort
template using only operator<() and swap() just to show that this
really works.
urs