Constant and Strings Flashcards
named constant
a constant value that is associated with an identifier
const int magic = 5;
Literal constant
a constant value that is not associated with an identifier
“Hello”
constant expression
an expression that can be evaluated at compile-time
runtime expression
any expression that is not a constant expression
compile-time constant
is a constant whose value is known at compile-time
runtime constant
a constant whose initialization value isn’t known until runtime
constexpr
must be a compile-time constant, and initialized with a constant expression. Function parameters cannot be constexpr
Inline expansion
a process where a function call is replaced by the code from the called function’s definition
What does an inline function request from the compiler?
Give code example of inline function
A function that the compiler is requested to expand in-line at each point the function is called, instead of performing a regular function call. The compiler may choose to ignore this request
inline int add(int a, int b) {
return a + b;
}
Two primary requirements of inline functions
The compiler needs to be able to see the full definition of an inline function or variable in each translation unit where the function is used (a forward declaration will not suffice on its own). The definition can occur after the point of use if a forward declaration is also provided.
Every definition for an inline function or variable must be identical, otherwise undefined behavior will result.
inline variables
variables that are allowed to be defined in multiple files
what does the term inline mean in modern c++
multiple definitions are allowed
constexpr function
function whose return value may be computed at compile-time
When will constexpr functions be evaluated
Constexpr functions are only guaranteed to be evaluated at compile-time when used in a context that requires a constant expression. Otherwise they may be evaluated at compile-time (if eligible) or runtime.
consteval function
a function that must evaluate at compile-time