Magmai, the evil moderator, decided to close the "C++ or C#"-topic for no one''s benefit. Since your (sjelkjd) email isn''t visible, I had to make a new topic for a reply. I hope the moderators are happy.
quote:Original post by sjelkjd
How would you implement sort in java? It has to take two iterators, and sort the range with quicksort. Maybe I''m wrong here, but I thought you couldn''t swap the elements of two iterators in java.
You''re right, I really hadn''t thought it out throughoutly. In Java, you''d need some kind of assign(Object o)-method for the Object class to accomplish it. Now the iterator class can''t affect the objects it points at, since none of the Object''s methods change the object''s contents.
quote:But with java, you can only do things that the iterator interface supports.[...]
Your Addable-argument is a good argument for why Java''s system doesn''t work. But it''s among the only arguments: C++ template algorithms benefit from the fact that C++ has some conventions. + is used for adding, copy constructor is used for copying. So templates can easily exploit these facts and hope to get good results for many kinds of objects.
But suppose you were to make a template algorithm that wants a vector class that supports taking length of the vector. Will the function be !vec, ~vec, vec.length() or vec.magnitude()? As soon as you stop using trivial operators, you''ll fall to the same level as Java programmers: You''ll have to implement the correct interface. In Java it requires you to use "implement", in C++ it requires you to provide function with a conforming signature.
quote:But, if you want to use it with other code, you have to write a wrapper around other people''s code to implement that interface.
Wrappers aren''t so uncommon in C++ algorithms either, due to the very same reason. std::less is a wrapper that uses the operator<() -interface. std::more is a wrapper that uses the operator>() -interface.