views:

44

answers:

1

What I want to do is, in a separate namespace, define my own sort(), copy(), etc implementations that work with database tables/views/etc containers instead of in-memory std containers. If I define my own sort() that accepts my custom forward iterator, how does the compiler resolve that? Or, what do I need to do so that it will resolve properly i.e. use my custom sort() instead of std sort(), even though I may be passing in custom iterator types that match the standard iterator type requirements?

If it uses std sort(), it would actually still work, it would just be extremely inefficient because it wouldn't be handing over the sort to the database, whereas my sort() implementation would. I'm just not sure how to properly invoke my sort() over std sort() when they have the same name and accept the same types. I may have missed some details in my description, so please bear with me on bludgeoning my way through this issue.

Also, I found the following question to be the most similar to what I'm asking (mentions ADL and partial specialization), but I'm not sure if it directly addresses my issue or describes the best way to go about doing what I've described: http://stackoverflow.com/questions/1988978/overloading-for-each-for-specific-iterator-types

+3  A: 

It's actually defined behaviour to specialize std namespace algorithms for your own UDTs.

namespace std {
    template<> void sort<sometype::someiterator>(sometype::someiterator begin, sometype::someiterator end) {
    ...
    }
};

Edit: Oopsie Sort instead of sort.

Edit again: Oh man, I wrote something totally wrong. That's not explicit spec syntax at all.

DeadMG
Ok, I think I understand, so correct me if I get this wrong. RIterator would end up being my custom iterator to my DBContainer, correct? So basically I'm telling the compiler, "I know you already have a generic sort(), but this sort() will handle these particular types better than your sort()." Could I define that sort in a different namespace and get the same result?
pheadbaq
@pheadbaq: Sorry, intent was right but code wrong. You are right, the compiler will pick your specialization over the generic std::sort. You could define it in another namespace, but you'd have to also define a generic sort (this is a specialization of an existing function) and in addition, you'd lose compatibility with third-party code and it would confuse the hell out of everyone on your team not using the normal sort.
DeadMG
@DeadMG: Please bear with me, new to specialization issues :\ So if I define my custom sort in std namespace, then declare "using std", then when I pass my custom iterator to sort without fully qualifying it, the compiler will pick up my specialized version. However, if I define sort in some other namespace "foo", then declare both "using std" and "using foo" and call unqualified sort and pass it my custom iterators, this breaks my ability to have the correct sort called if I hand my iterators to 3rd party code that uses std::sort, and my team has to explicitly define which sort? That right?
pheadbaq
@DeadMG: I think it's becoming clearer... if I stick to std namespace, I can just provide my specialized version. If I break out of std namespace, in order to have just one sort called for everything, I have to provide my specialization AND a replacement generic sort, and that new generic sort STILL wouldn't be called by 3rd party stuff, so now I'm dealing w/ differences between my generic sort and theirs. That jive with what you said?Thanks for the explanation and code, this is helping a lot!
pheadbaq
@pheadbaq: Yes, that's exactly it. For best effect, just specialize the existing std::sort.
DeadMG