By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

static object initialization

P: n/a
Hi,

this is an STL newbe question, sorry if it is too stupid:

Is there a way to statically _initialize_ an STL map, i.e. not at runtime?

There seems to be a map constructor that expects a start and an end
iterator, which
gives me some hope. But then I need something to iteate upon, and as
much as I
understand, this must be an array of STL pairs that match the map's
template
specification. Thus, could my problem be reduced to creating an array of
pairs,
and if yes, how do I statically create _and_ initialize an array of
objects (i.e. pairs)?

Thanks for any help,

Christof

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Christof Warlich wrote:
this is an STL newbe question, sorry if it is too stupid:

Is there a way to statically _initialize_ an STL map, i.e. not at runtime?


No. In reality only PODs can be initialised at program loading. All
other types have to be constructed and that can only be done at run-time.

V
Jul 23 '05 #2

P: n/a
Christof Warlich wrote:
Is there a way to statically _initialize_ an STL map, i.e. not at runtime?


I should have looked at the FAQ first, sorry
(http://www.parashift.com/c++-faq-lite/ctors.html):

Fred a[10] = {
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7),
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7)
};

So here is the solution for the map:

#include <map>
#include <iostream>

pair<const char *, int> a[] =
{
pair<const char *, int>("willi", 3),
pair<const char *, int>("max", 4),
};

map<const char *, int> myMap(a, a + sizeof(a) / sizeof(a[0]));

int main() {
cout << myMap["willi"] << "\n";
cout << myMap["max"] << "\n";
return 0;
}

Anyway, thanks for helping.

Regards,

Christof

Jul 23 '05 #3

P: n/a
Christof Warlich wrote:
Christof Warlich wrote:
Is there a way to statically _initialize_ an STL map, i.e. not at
runtime?

Not at run-time means at compile time. The answer is still "No".
I should have looked at the FAQ first, sorry
(http://www.parashift.com/c++-faq-lite/ctors.html):

Fred a[10] = {
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7),
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7)
};
If 'Fred' is a non-POD, which is very likely, every object has to still
undergo initialisation by a constructor, which means run-time.
So here is the solution for the map:

#include <map>
#include <iostream>

pair<const char *, int> a[] =
{
pair<const char *, int>("willi", 3),
pair<const char *, int>("max", 4),
};

map<const char *, int> myMap(a, a + sizeof(a) / sizeof(a[0]));

int main() {
cout << myMap["willi"] << "\n";
cout << myMap["max"] << "\n";
return 0;
}


That's a solution for static map initialisation, but it involves run-time
nonetheless. The constructors for 'myMap' elements *will* be invoked b4
'main' is called, but *not* during compile time, mind you.

V
Jul 23 '05 #4

P: n/a
Victor Bazarov wrote:
That's a solution for static map initialisation, but it involves run-time
nonetheless. The constructors for 'myMap' elements *will* be invoked b4
'main' is called, but *not* during compile time, mind you.

V


Hi Victor,

you are right, the wording in my question was misleading. In fact,
I was just only looking for an initialization outside of main.
So I'm happy now.

Thanks again,

Christof
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.