Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confused about compile time constants

As I read in a C++ book, the process of gcc is: Pre-Processing, Compiling, Assembling and Linking.

There are Compile Time constants and Run Time constants. Then I get confused about "Compiling" and "compile time". Please tell me what "compile time" means in the process of gcc, is it the whole of Pre-Processing, Compiling, Assembling and Linking?

like image 748
fizzbuzz Avatar asked Nov 19 '25 10:11

fizzbuzz


1 Answers

"Compile Time" or "Compiling" is one part of the process. In C++, there is a feature called Macros that are expanded into C++ code during the Pre-Processor stage. This Pre-Processor stage runs first before the compiler. Depending on the compiler, the next step is to convert the human-readable C++ code to another form that is readable by machines. This really depends on the type of the compiler and other possible arguments passed in during compilation as to what this turns into. For sake of simplicity, let's assume the compiler compiles the C++ to symbols/assembly/machine code. Each of these files is independent, for lack of better term.

The last step is to 'link these together. Includes, forward declarations, and externs are resolved during this stage. The once independent files now 'linked' with sizes of types/classes/structs known at the end of the stage. After all this is done, an executable is formed for which is purely machine language and understood by the computer running the program.

A general rule of thumb for C++ programming is you want to catch as many errors as possible at compile time. Run-time is the span of time for which your program is executing the machine code. This can include heap allocations, dynamic list resizing, etc...

Now, to the crux of your question. What is the difference between a Run-Time constant and Compile Time constant? It is quite simple. Compile Time constants are values known and resolvable at compile time. These values can be determined and calculated and depend on no external user input or file data. See constexpr as a good reference for learning more about compile time constants: ConstExprLink.

const int userScore = 3; // Compile time constant
const int secondUserScore = 5; // Compile time constant
constexpr int CombinedUserScores = ( userScore + secondUserScore ); // Also compile constant because all values are known at compile time   

However, there are some constants that cannot be known at compile time. These values can be determined and resolved after your program Run-Time begins. Now, in contrast, imagine you are reading user input or data from a file during Run-Time

std::cout << "Enter your Test Score: ";
const int testScore = 0;
std::cin >> testScore; // User sets an input value during Run-Time

const int userScore { testScore }; // Run-Time constant. No way to know the value at compile time

userScore is now a Run-Time constant that cannot be changed unless you engage in some taboo stuff like const_cast or C style casts. The point here is that the compiler has no way to know what the value of this constant would be until the input is read in. This is often called Data-Driven programming and is a very common paradigm. Also, because the Run-Time const is not determined at compile time, constexpr cannot be used here.

The above is an example of a Run-Time constant. This value cannot be resolved at compile time because the compiler has no idea what the input from the user or file might be. Here is another good reference: CompileTimeConstant Versus RunTimeConstant

like image 144
Paul Renton Avatar answered Nov 21 '25 22:11

Paul Renton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!