Preprocessor and Build Boundaries
Constexpr Config
constexpr names a value that can be used at compile time, while ordinary variables still drive runtime decisions.
Constexpr Config
constexpr_config.cpp
#include <array>
#include <iostream>
int main() {
constexpr int freeLimit = 3;
int used = ;
std::array<int, freeLimit> included{1, 2, 3};
bool overLimit = used > freeLimit;
std::cout << "freeLimit=" << freeLimit << std::endl;
std::cout << "includedSlots=" << included.size() << std::endl;
std::cout << "used=" << used << std::endl;
std::cout << "overLimit=" << overLimit << std::endl;
return 0;
}
#include <array>
#include <iostream>
int main() {
constexpr int freeLimit = 3;
int used = ;
std::array<int, freeLimit> included{1, 2, 3};
bool overLimit = used > freeLimit;
std::cout << "freeLimit=" << freeLimit << std::endl;
std::cout << "includedSlots=" << included.size() << std::endl;
std::cout << "used=" << used << std::endl;
std::cout << "overLimit=" << overLimit << std::endl;
return 0;
}
#include <array>
#include <iostream>
int main() {
constexpr int freeLimit = 3;
int used = ;
std::array<int, freeLimit> included{1, 2, 3};
bool overLimit = used > freeLimit;
std::cout << "freeLimit=" << freeLimit << std::endl;
std::cout << "includedSlots=" << included.size() << std::endl;
std::cout << "used=" << used << std::endl;
std::cout << "overLimit=" << overLimit << std::endl;
return 0;
}
constexpr
A `constexpr` variable is known during compilation when its initializer is also a constant expression.