|
Boost : |
From: meadori_at_[hidden]
Date: 2006-06-29 17:35:08
> I suppose static maps will make a lot of sense if you need to deal
> with it in template metaprogramming or even preprocessing...
> Otherwise, it's just another way of implementing a simple map.
They make sense in other ways as well. For the scenario static_map
is trying to solve you only want the map initialized once. I guess you
could use boost.assign with a static object. Or you could right your own
init code. For example:
// namespace scope
struct name_age_map {
std::map<std::string, int> map;
name_age_map() {
this->map["Will"] = 28;
this->map["Joe"] = 30;
}
};
static name_age_map my_map;
// class scope
class foo {
static name_age_map my_map;
};
name_age_map foo::my_map;
// function scope
void bar() {
static name_age_map my_map;
}
I have three gripes about this solution:
1. The scaffolding code is a pain to write over and over
2. It is not thread safe
3. You may not want to pay the runtime initialization cost
if the map is huge
.
A static_map would solve all these problems. And possibly more?
--Meador
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk