I think I'm going to have to buy some of my regular responders a Christmas present for all the help I've received lately! Alas tho I have yet another question, and there are many many more to come. The road is long, with many a winding turn as they say ;oD
ANyway check out this code:
struct Popless : public std::binary_function<State, State, bool> { bool operator () (const State &a, const State &b) const {return popLess (a, b);} // popLess is an inline function};
Ok now normally this would seriously FREAK me out. I also know Zahlman hates me calling it the STL but I don't know how else to refer to it ;o) It isn't quite as scary as it should be tho anymore. Probably because I'm doing alot of C++ studying lately.
Anyway down to business. 'State' is just a user defined class that refers to a state in good old USA. The code is vaguely based around population comparison. The guy wants to make an STL function object that he can pass to the std::sort function. As far as I know tho - std::sort requires a function pointer or function object to be passed to it as one of it's arguments. Well obviously Popless isn't a function it's a struct. But it does make use of the function call operator overload and I am told that this means it will make the grade required to be used in an std::sort operation.
Odd really, I can only assume then that std::sort only looks to see how the argument passed to it is triggered/initiated. It musn't actually look to see what exact details belong to the argument being passed to check to see if it's a correct entity or anything, it just wants to know how to start it. Makes sense really I suppose, I guess anything that was too inquisitive might cause a load of unnecessary operations to occur at a low level. If I'm talking bull please correct me - my education would benefit ;o)
Anyway - std::sort doesn't panic when passed a structure - provided it has an operator () overload defined it it. Ok fair enough. Now noting that popLess (get's confusing here, this is the actual function call and it is supposed to be inline) is the function actually triggered, the author states that this will retain its inline and thus its speed benefit. I understand this would NOT happen if popLess were simply used in the std::sort call, and you would lose the inline benefits of it.
I believe the author made this entire function object purely for this purpose. So basically after all that stuff I have these questions:
1)Does calling it this way preserve the inline speed of popLess?
2)Does the fact that all this extra insulating code exists - the Popless struct stuff - negate any speed benefit of preserving the inline property of popless?
3)Apparently the shrewd choice of inheriting from std::binary function adds the benefit of being able to find out the argument and return types of the function object Popless. Hmmm.... ok but why would we want to do that? The author says it's something to do with allowing capability for other users. Why don't those other users just go and check out the definition in the IDE and write their code to suit?
MY brain is very sore so I'm going to stop there. I hope that's made some sense, I am trying as hard as I can to understand it, but it does feel a bit new and I wonder if I'm talking buloney sometimes. Anyway - thanks in advance for any help anyone can offer ;o)