Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are 'const' variables precomputed by default in C++?

Tags:

c++

Suppose I have variables for positions like

const float latitude = 51.+11./60.+33.0461/3600.;
const float longitude = 12.+50./60.+31.9369/3600.;

and use them frequently in the program. Does the compiler precompute that? (This example should not produce much overhead, but you get the point.)

Bonus point for pointing out location. ;)

TIA

like image 773
Gunnar Avatar asked Dec 17 '22 00:12

Gunnar


1 Answers

I don't believe it is required for the compiler to compute the result of arithmetic constant expressions in general.

The compiler is, however, required to compute the result of an integral constant expression (basically, a constant expression composed only of integers and other values converted to integers) in cases where it needs the result--that is, when an integral constant expression is used as an array size, as a case expression, as an enumerator value, etc.

I'd be surprised, however, if any modern compiler didn't compute the result of a constant expression.

like image 169
James McNellis Avatar answered Dec 24 '22 01:12

James McNellis