GCC Explorer - an interactive take on compilation
Posted May 28, 2012 14:28 UTC (Mon) by
jwakely (subscriber, #60262)
In reply to:
GCC Explorer - an interactive take on compilation by etienne
Parent article:
GCC Explorer - an interactive take on compilation
IMHO C++ pushes a lot of things from the .data section (i.e. initialised variables/structures) into runtime execution/initialisation (object constructors), when compared to C.
Do you have anything more convincing than opinion? IMHO C++ puts the same things in .data, when compared to C.
C++ allows you to initialize globals in more ways than C, and those things might need dynamic initialization, but they are things that must also be dynamically initialized in C. Unless you know some way to statically execute an object's constructor in C that can't be done in C++?
Moreover some obvious optimisations cannot be done because objects "constant during execution" are still not initialised at compilation time, so compiler cannot do anything about them.
Have you looked at the C++11 keyword constexpr? It's specifically designed to allow such initialization.
struct Data {
constexpr Data(int i, int j) : i(i), j(j) { }
int i;
int j;
};
constexpr Data data(1, 2); // statically initialized
This example is clearly pointless (an array of two ints would serve in this case, and be statically initialized in C or C++03) but there are many more things you can do with
constexpr.
(
Log in to post comments)