static object initialization

C

Christof Warlich

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
 
V

Victor Bazarov

Christof said:
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
 
C

Christof Warlich

Christof said:
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
 
V

Victor Bazarov

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
 
C

Christof Warlich

Victor said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,052
Latest member
EvaW192252

Latest Threads

Top