473,386 Members | 1,745 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,386 software developers and data experts.

Compile time initialization of data.


typedef unsigned long long int uint64
typedef unsigned char uint8;

Class Simple
{
union { uint64 x; uint8 r[8]; }

public:
Simple(uint64 n) : x(n) {;}
//....
};
Class Simple_user
{
static const Simple simple_array[8];

public:
//....
};

const Simple Simple_user::simple_array[8] =
{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
Will the array of simples always be constructed at compile time?
Can an optimizing compiler construct the array at compile time?

Thanks for any answers/comments.

--
Regards,
S.K.Mody
Aug 3 '05 #1
7 2526
S.K.Mody wrote:
typedef unsigned long long int uint64
Not valid C++ (yet). There is no 'long long' type in C++. You
should probably have used 'double'.
typedef unsigned char uint8;

Class Simple
class Simple

, maybe?
{
union { uint64 x; uint8 r[8]; }

public:
Simple(uint64 n) : x(n) {;}
//....
};
Class Simple_user
class Simple_user

, maybe?
{
static const Simple simple_array[8];

public:
//....
};

const Simple Simple_user::simple_array[8] =
{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
Will the array of simples always be constructed at compile time?
No. It's unspecified (8.5.1/14).
Can an optimizing compiler construct the array at compile time?


It may. Whether a particular one _can_ or not, depends on its
implementors, doesn't it?

V
Aug 3 '05 #2
Victor Bazarov wrote:
S.K.Mody wrote:
typedef unsigned long long int uint64
Not valid C++ (yet). There is no 'long long' type in C++. You
should probably have used 'double'.


In the actual code, I used the C99 header provided with
glibc (stdint.h) which has nice fixed width types like
uint16_t, uint64_t, int_fast8_t etc, which may make it to C++
eventually. I'll have to use some preprocessor conditionals or
roll my own uint64 if I decide to compile it for other platforms.


Will the array of simples always be constructed at compile time?


No. It's unspecified (8.5.1/14).
Can an optimizing compiler construct the array at compile time?


It may. Whether a particular one _can_ or not, depends on its
implementors, doesn't it?


True, but I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.

I'm using g++ on x86 Linux.
V


--
Regards,
S.K.Mody
Aug 4 '05 #3
S.K.Mody wrote:
[..] I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.
There is no other "agreement" among compiler writers except the
Standard Document, I hope.
I'm using g++ on x86 Linux.


Good for you. It doesn't matter here, though.

V
Aug 4 '05 #4
Victor Bazarov wrote:
S.K.Mody wrote:
[..] I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.
There is no other "agreement" among compiler writers except the
Standard Document, I hope.


Why do you hope? Would there be any problems if a subset
of the set of all compilers behaved somewhat predictably in
some respects even though the exact behaviour is left
unspecified by the standard?

V


--
Regards,
S.K.Mody
Aug 4 '05 #5
S.K.Mody wrote:
Victor Bazarov wrote:
S.K.Mody wrote:
[..] I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.


There is no other "agreement" among compiler writers except the
Standard Document, I hope.


Why do you hope? Would there be any problems if a subset
of the set of all compilers behaved somewhat predictably in
some respects even though the exact behaviour is left
unspecified by the standard?


Yes. The problem is simple: if there is nothing _governing_
the behaviour, nothing is there to prevent it _change_ some
sunny day, and therefore none of it can be _relied upon_. What
else did you expect me to tell you?
Aug 4 '05 #6
Victor Bazarov wrote:
S.K.Mody wrote:
Victor Bazarov wrote:
S.K.Mody wrote:
[..] I am hoping that there is some agreement among compiler
writers as to what constitutes an acceptable level of optimization.
After all, there is technically no requirement for example that
a compiler inline any methods at all, but for some types of code
that would prove to be unacceptable.

There is no other "agreement" among compiler writers except the
Standard Document, I hope.


Why do you hope? Would there be any problems if a subset
of the set of all compilers behaved somewhat predictably in
some respects even though the exact behaviour is left
unspecified by the standard?


Yes. The problem is simple: if there is nothing _governing_
the behaviour, nothing is there to prevent it _change_ some
sunny day, and therefore none of it can be _relied upon_. What
else did you expect me to tell you?


I think the philosophy of C++ provides the governing principle
- To achieve the right balance between portability and efficiency.
For example without inlining, to which the original question is
closely related, it would often be unacceptably inefficient to
have deeply nested calls to small functions. But such calls may
be necessary for a variety of reasons related to good C++ design.
So should one go back to writing macros and forget about design
principles or can one compromise a little and ask for some informal
guarantees from the specific compiler (or class of compilers)
that one may be working with? The latter seems to me to be a better
option - since the choice is unmaintainable spaghetti code v/s
well designed code with some compiler specific preprocessing.

You may regard this as a strictly compiler related question but
it seems to me that the C++ efficiency goals virtually require
the compiler to provide such informal albeit non-portable
guarantees. The original question could therefore be rephrased
as "Is there any sort of uniformity among compilers in this regard?"
I'm not sure whether your answers were based on specific knowledge
of widely varying implementations or on the legal position of the
standard.

--
Regards,
S.K.Mody
Aug 4 '05 #7
S.K.Mody wrote:
I think the philosophy of C++ provides the governing principle
- To achieve the right balance between portability and efficiency.
For example without inlining, to which the original question is
closely related, it would often be unacceptably inefficient to
have deeply nested calls to small functions. But such calls may
be necessary for a variety of reasons related to good C++ design.
uh-huh.
So should one go back to writing macros and forget about design
principles or can one compromise a little and ask for some informal
guarantees from the specific compiler (or class of compilers)
that one may be working with? The latter seems to me to be a better
option - since the choice is unmaintainable spaghetti code v/s
well designed code with some compiler specific preprocessing.
If you know that you will never compile your program on anything other than the compiler you're using today, do what you like. But rest-assured that there are lots of people who
expected their code to not still be around 20 years later. (Such as that in banking systems... remember Y2K? It not like the programmers didn't know about the problem, they just
expected the code to not be around when the problem surfaced).

The point is that one day, somebody might try to re-use the code (I have heard of it happening in the past). They may not expect it to rely upon on-standard behaviour.
You may regard this as a strictly compiler related question but
it seems to me that the C++ efficiency goals virtually require
the compiler to provide such informal albeit non-portable
guarantees.
Not in the slightest. Optimisation is nice, but there is loads of code that relies upon side effects that should not exist, and optimising that code breaks it. Again, reliance
upon non-standard behaviour (such as doing anything in a copy-constructor that is not purely related to copying the object), might break your code some day.
The original question could therefore be rephrased
as "Is there any sort of uniformity among compilers in this regard?"
Yeah, of there is. But thats NOT the question. The question is "Should you rely upon it?"
I'm not sure whether your answers were based on specific knowledge
of widely varying implementations or on the legal position of the
standard.


Probably a bit (or more) of both.

Ben
--
I'm not just a number. To many, I'm known as a String...
Aug 8 '05 #8

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

Similar topics

19
by: Christopher | last post by:
I am getting a parse error from g++ pointing at my catch line and can't figure out whats wrong with this code: #include "BigPosInt.h" #include <iostream> #include <new> #include <assert.h> ...
6
by: stef | last post by:
Anyone have any guesses as to why I am getting an error message when I try to compile: In file included from CP.h:13,from driver.cpp:5: cb.h:33: invalid data member initialization cb.h:33: (use...
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
11
by: babuyama | last post by:
Hi, Is there a way to obtain library name at compile/preprocessor time? Assuming that the compilation unit, myfile.c is part of mylib.a, from myfile.c code at compile/preprocessor time, I would...
16
by: John Kelsey | last post by:
Back in the "old" days with C, I used to do something like... struct { char Description; float price; } Items = { {"Apple", 1.99}, {"Banana", 2.04}
5
by: Arno | last post by:
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...
22
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved...
11
by: Tim H | last post by:
The following program compiles just fine in C, but not in C++. Can anyone explain why? I have a chunk of code that defines stuff like this in headers (without the extern) that I can not easily...
2
myusernotyours
by: myusernotyours | last post by:
Hi All, Am working on a Java application in which I have to use the JNI to Interface with some native code for both windows and unix. Am using netbeans IDE with the C/C++ pack installed. Am also...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.