Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Initializing a map...
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="James Kanze, post: 3451043"] Or even: std::map< int, int > z = myMap< int, int >()( 3, 4 )( 5, 6 ) ; I like it. I'm just afraid that it deviates enough from usual practice to be a bit of obfuscation. (Maybe you should try to get something like this into Boost, so that it can be considered usual practice.) Note too that as used above, there is a deep copy of the map. Not necessarily the sort of thing you might want in a tight loop. (But how often does one construct pre-initialized maps in a tight loop, anyway?) The other thing to consider is that it binds the initializer list very tightly to the actual declaration, in a way which makes machine generation of the initializers somewhat difficult. And if the map is small enough that you're willing to consider writing the initialers out by hand, it's probably small enough for you to simply use find_if on a C style array. It's true that std::map isn't designed to be used as a base class. But you have to weigh everything. It doesn't bother me in the least to derive from it to provide a specialized constructor which contains the specific initialization---the semantics of the resulting class (with its specific initialization) are such that no one is going to use it except in its intended use. His class is more general, so perhaps the risk is greater, but I still find it within the realm of reasonableness. On the other hand... std::map basically provides an implementation class for several more or less distinct use cases. (See the discussions on its operator[], which can't be used on a const object.) Perhaps the ideal solution is to define precise interfaces for each of the use cases, using a member std::map for the implementation. I'd wonder about any code which allocated an std::map dynamically to begin with. I don't think the risk here is very great. In both cases, this means a lot of brunt typing to duplicate the interface. It might be worth it, however. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Initializing a map...
Top