473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Complex objects initialized at compile time

Hello,

here is my problem: I have a complex data type, like a std::vector<
std::string > and need some global constant values of this type. The
type is also used non-constant throughout the program, so I do not want
to use dumb types, but rather STL things.

In the best of worlds, initialization of these constants would not
happen at runtime, but at compile time, essentially generating a
constant memory image of this type via meta-programming. Has anyone
made any attempts in this direction?

I have written some simple macros that do it in Visual C++ 2005 for
std::vector< integral_type >. Of course, it is non-portable, but the
general principle _would_ be portable, with merely the exact memory
arrangement to be non-portabel.

Any ideas?

Arno

Feb 12 '06 #1
5 2201
"Arno" <as******@think-cell.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hello,

here is my problem: I have a complex data type, like a std::vector<
std::string > and need some global constant values of this type. The
type is also used non-constant throughout the program, so I do not want
to use dumb types, but rather STL things.

In the best of worlds, initialization of these constants would not
happen at runtime, but at compile time, essentially generating a
constant memory image of this type via meta-programming. Has anyone
made any attempts in this direction?

I have written some simple macros that do it in Visual C++ 2005 for
std::vector< integral_type >. Of course, it is non-portable, but the
general principle _would_ be portable, with merely the exact memory
arrangement to be non-portabel.

Any ideas?

Arno


I think one of the problems you will come across is the fact that
std::string does not store the character data in the class object itself,
but a pointer to the data. So at run time the memory will need to be
allocated for the char data, and the std::string pointer updated to point
to it. I don't see how this would be possible at compile time.
Feb 12 '06 #2
All STL containers contains pointers to allocated memory, but this
problem can be solved quite easily. I simply define the chunk (a string
for std::string or an array for std::vector) as const, and then
initialize the main structure with pointers to that chunk. It all
compiles to a constant, without run-time initialization. Since the
structure is const, the "pseudo-allocated" memory never gets
reallocated or deallocated, so the fact that the memory lies in the
code section rather than on the heap makes no difference to the
program.

The whole thing becomes tricky because complex structures would contain
many levels of such indirection. In case of std::vector< std::string >
, the vector points to its internal buffer containing std::string, which in turn have buffers for their string data. This would have to be defined in an orderly fashion, first raw strings, then the array of std::string structures pointing to the strings, then the vector pointing to the array. Hence the need for template metaprogramming.


Feb 12 '06 #3
In article <11**********************@g44g2000cwa.googlegroups .com>,
"Arno" <as******@think-cell.com> wrote:
Hello,

here is my problem: I have a complex data type, like a std::vector<
std::string > and need some global constant values of this type. The
type is also used non-constant throughout the program, so I do not want
to use dumb types, but rather STL things.

In the best of worlds, initialization of these constants would not
happen at runtime, but at compile time, essentially generating a
constant memory image of this type via meta-programming. Has anyone
made any attempts in this direction?

I have written some simple macros that do it in Visual C++ 2005 for
std::vector< integral_type >. Of course, it is non-portable, but the
general principle _would_ be portable, with merely the exact memory
arrangement to be non-portabel.

Any ideas?


I suggest you look at using a class like Stroustrup's c_array (from "The
C++ Programming Language") and make a c_string class as well.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 12 '06 #4
The problem with this approach is that it makes const and non-const
types incompatible with each other. Say I have a struct _numberformat,
that stores prefix and suffix to a number as a std::string. Now I want
to a) use a few constant items of this type for default _numberformats,
and b) allow user-defined _numberformats, which are non-const. But when
calling a function datafield.SetNumberFormat( _numberformat const& ), I
want to be able to supply either const or non-const _numberformats.

Feb 12 '06 #5
In article <11**********************@g47g2000cwa.googlegroups .com>,
"Arno" <as******@think-cell.com> wrote:
The problem with this approach is that it makes const and non-const
types incompatible with each other. Say I have a struct _numberformat,
that stores prefix and suffix to a number as a std::string. Now I want
to a) use a few constant items of this type for default _numberformats,
and b) allow user-defined _numberformats, which are non-const. But when
calling a function datafield.SetNumberFormat( _numberformat const& ), I
want to be able to supply either const or non-const _numberformats.


Templates to the rescue. The only variability is on types, either a
c_string class or a string class...

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dado | last post by:
I lost the point: What is purpose of putting objects to null: If I make a class, JFrame for example, with button which start connection dialog, which is class with connection and statement...
3
by: joe martin | last post by:
Does anyone know when or if destructors for statically declared objects are called? I am just wondering if I am leaking system resources each time i exit my program. Thanks, -Joe ...
6
by: glen_stark | last post by:
Hi. I'm just curious if there any warnings or caveats or whatever to be aware of when inlining function object calls? If the answer is no, they inline just like everything else, that's good...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
15
by: Jakob Bieling | last post by:
Hi, I am aware of the fact, that the order of construction of global objects is unspecified. But I am in a situation, where I need to guarantee, that one object is created before others (not all...
3
by: Dennis M | last post by:
Hey everyone, I am curious what the performance impact of a custom control would be if it had a significant hierarchy of children. For example, 40 child controls, 5 levels deep, each control on...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
14
by: ablock | last post by:
I have an array to which i have a added a method called contains. I would like to transverse this array using for...in...I understand fully that for...in is really meant for Objects and not Arrays,...
2
by: =?Utf-8?B?ZHNoZW1lc2g=?= | last post by:
Hello, My problem is a little complex... Here it goes: I have a web service in my system which my users need to web reference. By web referencing my web service, my users can use all kinds of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.