TITLE: warning on const float but not const int PROBLEM: orfanos@ima.enst.fr (Dimitri Papadopoulos Orfanos) My compiler (Borland C++ 4.5) exhibits different behaviour between 'const float' and 'const int' declared in header files: --- MAGWEG.H --- const float FOO1 = 3.14159; const int FOO2 = 2; const char* FOO3 = "Hello world!"; --- MAGWEG.CPP --- #include "magweg.h" int main() { return 0; } --- compiler output --- Compiling MAGWEG.CPP: Warning MAGWEG.CPP 8: 'FOO1' is declared but never used Why does the compiler makes a distinction between 'const float FOO1' and 'const int FOO2'? Is there any particular reason for it? RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 18 Feb 96 A const object does not have external linkage unless it is declared extern, so none of the FOO objects have external linkage. The compiler is not required to generate storage for such objects if it doesn't need to -- that is, if the object is not passed by reference and its address is not taken and the compiler does not need to instantiate it for some other reason. (An object with a nontrivial constructor or destructor must be instantiated.) In the case of an int constant, the compiler will typically substitute the value for the variable wherever it is used, since on most systems this results in smaller and faster code. FOO2 is never instantiated. Some systems do not allow literal floating-point values in machine instructions, so the compiler has no choice but to generate a value in memory and refer to it in instructions. Alternatively, even if floating literals are allowed, the generated code might be smaller if a pointer to the in- memory value were used instead. In such cases FOO1 will be generated in memory unconditionally. Notice that all this follows from the "as-if" rule. If your program can't tell whether the object was instantiated, it doesn't matter whether it is actually instantiated or not. As an optimization, the compiler can choose not to intantiate such objects, using any criteria it likes.