views:

581

answers:

4

Map type from STL have next type:

std::map< Key, Data, Compare, Alloc >

As one of template parameters we could pass Compare predicate, why map accept this predicate as template parameter and not as object in constructor?

It could has more flexible interface with something like boost::function< bool, const T&, const T& > in constructor.
Ofcourse I'm understend that when STL was designed boost does not exists, but designers could be create something similar on boost::function.

I belive it has some deep reasons.

EDITED
Sorry for dummy question, map have same posibility :)
My question doesn't have sense after your answers.

+3  A: 

Because boost::function is polymorphic, it cannot be inlined. The design of the STL is aimed at maximum potential for the compiler to do inlining of code, which is easy on expanded templates; also you could easily make the decision to use boost::function to supply the comparison with std::map if you needed it to be polymorphic.

Daniel Earwicker
nice point, but std solution is more nice)) thank you for help.
bb
I think the second part of your question about "deep reasons" is perfectly valid, not dumb! :) By the way, std::tr1::function is already semi-standard, see http://en.wikipedia.org/wiki/Technical_Report_1
Daniel Earwicker
+5  A: 

Map DOES have such a constructor. From section 23.3.1 of the C++ Standard:

explicit map(const Compare& comp = Compare(),
const Allocator& = Allocator());
anon
+1 - Thank you. But sorry, I've accepted Rob's answer because he was early than you.
bb
Actually, Neil's answer was earlier by about 3 minutes, so if you're using submission time as a tie-breaker for otherwise equally helpful answers, then please award the acceptance to this answer.
Rob Kennedy
It really doesn't matter!
anon
+7  A: 

The template argument is the type of the predicate, not the value. The value can be provided as an argument to the constructor. You can specify any value that matches the type. As given, the default type is std::less<Key>, which pretty much only has one value, but you should be able to specify your own type for the Compare argument, including boost::function, and then use various values to control the behavior of your map objects.

Rob Kennedy
Ok. I am newbie on this site and don't know rules about awarding. But I suspect it depended from speed. Anyway, thank you for your answer.
bb
+2  A: 

Using a compare object creates a run-time cost - the object needs to be stored, and the comparison must occur through a pointer. By using a class, the comparison can simplify down into a single expression, for example when using int keys. The goal of the standard library was to be no less efficient than what a good C++ programmer would generate on their own.

Mark Ransom
'The goal of the standard library was to be no less efficient than what a good C++ programmer would generate on their own' -- Nice point too. +1 for remember me that.
bb