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: 3452476"] [...] In this case, the key is stored as a char const* more by habit than anything else. The underlying reason is that doing so allows static initialization, which in turn avoids any possible order of initialization issues. If the map and the table are defined in the same file (which is the case here), the order of initialization is well defined, and there is no problem. If the initialization table is defined in another translation unit, however (e.g. because it is machine generated), then static initialization means that it is still guaranteed to work; otherwise, you need some special logic to ensure that the table is initialized before the map. Similarly, if you expect the map to be used from constructors of other static objects, and use the singleton idiom to construct it, unless the initialization table uses static initialization, you might have an order of initialization problem. And of course, if the table is small enough to be initialized by hand, I'll use std::find_if on it, rather than bother creating a map. In such cases, ensuring static initialization also avoids any order of initialization problems, without the need of a singleton. (FWIW: ensuring static initialization is practically the only time I use char[] or char* instead of std::string.) [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Initializing a map...
Top