Questions about compile time constants

Started by
11 comments, last by alvaro 11 years ago

Do compile time constants exist only during compile time?

Ex:


const int x = 10;
int demo_array[x];

All I know is that x won't occupy storage,because the compiler can't access the storage.Instead,x is put into the symbol table.

And,is it possible for user defined types to become a compile time constants?

Advertisement
Assuming C++ here.


A constant can become zero-storage but isn't guaranteed to be so. For example, if you take the address of a constant, it basically is just another variable except you can't write to it at compile-time (barring evil like const_cast).

UDTs can be compile-time constants, sure. But they won't ever be zero-storage because that doesn't quite make sense :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

First of all that variable isn't guaranteed to be constant at all because you could declare it with extern elsewhere without the const and it could be accessed without const restrictions. If you want a value to be 100% constant it needs to be static const. In fact the lack of static is bound to give you compile-time errors if you try to use the value as a constant expression since, well, it isn't really constant (it's only so for the code within that file).

That aside, static const values in general are no storage, provided you don't do something stupid like taking a pointer to them. Things like structures and arrays take up memory for obvious reasons (though they'll be in the read-only area).

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Got it.Thanks

So,do compile time constants disappear after the compilation?

Assuming C++ here.


A constant can become zero-storage but isn't guaranteed to be so. For example, if you take the address of a constant, it basically is just another variable except you can't write to it at compile-time (barring evil like const_cast).

UDTs can be compile-time constants, sure. But they won't ever be zero-storage because that doesn't quite make sense :-)

Also worth pointing out that in some cases a constant can't become zero-storage - the most obvious and frequently-encountered such example being string constants.

Got it.Thanks

So,do compile time constants disappear after the compilation?

That depends on what exactly you mean by "disappear".

A few years ago I tried this with VS 2010 and one of the newer GCC version that time available for Windows. Both of them stored the constants, even if they didn't need to (the constant is not referred by address only by value). Actually when I accessed the constant in the header file from multiple source files the constant got stored multiple times. This can be problematic for global constants that are used in many files. Maybe the newer compilers / linkers are not so lazy about this, it might worth a test.

Assuming C++ here.


A constant can become zero-storage but isn't guaranteed to be so. For example, if you take the address of a constant, it basically is just another variable except you can't write to it at compile-time (barring evil like const_cast).

UDTs can be compile-time constants, sure. But they won't ever be zero-storage because that doesn't quite make sense :-)

Also worth pointing out that in some cases a constant can't become zero-storage - the most obvious and frequently-encountered such example being string constants.

>Got it.Thanks

So,do compile time constants disappear after the compilation?

That depends on what exactly you mean by "disappear".

Well,correct me if I'm wrong,but if I create a constant like:

const int x = 10;

Wherever I use that x,shouldn't the compiler just insert 10? If it's true,it inserts 10 wherever needed,but after that,why keep it?

Well,correct me if I'm wrong,but if I create a constant like:

const int x = 10;

Wherever I use that x,shouldn't the compiler just insert 10? If it's true,it inserts 10 wherever needed,but after that,why keep it?

As with many things, the compiler is free to do whatever it wants within the constraints of the language spec. As long as it can give you the expected behavior, it might take any approach that it wants. The approach could be due to performance, or ease of coding (on the compiler writers part), or whatever.

But, basically, unless you put constraints on what the compiler can do by referencing that variable in other places, then it could implement this in any way it sees fit.

Well,correct me if I'm wrong,but if I create a constant like:
const int x = 10;

Wherever I use that x,shouldn't the compiler just insert 10? If it's true,it inserts 10 wherever needed,but after that,why keep it?

The compiler can do that, but it doesn't have to; furthermore in certain situations, const variables MUST be treated as literals, in certain other situations, they can't be.

For example, with code like this:


const int x=10;
...
for (i=0; i<x; ++i){...}

the compiler can treat the variable x in the for statement as if it were a literal 10, although it is not required to. But, in this code:


const int x=10;
...
int array[x];

in C++, the x in the array declaration MUST be treated as a literal 10. In C, it may be, but isn't required to (at least under the C99 and C11 standards; under older standards, it simply won't compile).

On the other hand, if you do this:


const int x=10;
...
const int *px=&x;

the compiler is required to allocate storage for x, regardless of how it deals with x elsewhere in the code.

And while you may find yourself asking why you would take the address of a constant, here's a more familar-looking snippet of code that does just that:


std::string hw="Hello World!";

UDTs can be compile-time constants, sure. But they won't ever be zero-storage because that doesn't quite make sense :-)


Things like structures and arrays take up memory for obvious reasons (though they'll be in the read-only area).


I don't know what you guy means by "doesn't quite make sense" and "for obvious reasons". The following three programs generate the same executable byte for byte (using g++ 4.7.1):
#include <iostream>

int main() {
  std::cout << 5 << '\n';
}
#include <iostream>

int main() {
  int const i = 5;
  std::cout << i << '\n';
}
#include <iostream>

struct X {
  int x;
 
  X(int x) : x(x) {
  }
};

int main() {
  X const i = 5;
  std::cout << i.x << '\n';                                                                                   
}                                                                                                           

This topic is closed to new replies.

Advertisement