Phần 1
1. Goal
After learning constexpr, you should understand:
1. What compile-time evaluation means.
2. Why C++11 introduced constexpr.
3. The difference between const and constexpr.
4. How to write simple constexpr variables and functions.
5. Common mistakes when using constexpr.Phần 2
2. What Problem Does It Solve?
constexpr means a value or function can be evaluated at compile time.
Before C++11, compile-time constants were often written with macros or simple const variables.
#define MAX_ORDERS 1000
const int maxOrders = 1000;C++11 introduced constexpr to express compile-time constants more clearly and safely.
constexpr int maxOrders = 1000;Main idea:
Use constexpr when a value should be known at compile time.Phần 3
3. Minimum Syntax
Compile-time variable:
constexpr int maxOrders = 1000;
constexpr double tickSize = 0.01;Compile-time function:
constexpr int square(int x) {
return x * x;
}Use it:
constexpr int value = square(10);Phần 4
4. const vs constexpr
const means the variable cannot be modified after initialization.
const int x = 10;constexpr means the value can be known at compile time.
constexpr int x = 10;Simple rule:
const = cannot be changed
constexpr = compile-time constant when possibleNot every const is compile-time.
int n;
std::cin >> n;
const int x = n; // const, but not compile-timePhần 5
5. Common Mistakes
5.1. Thinking all const values are constexpr
int n;
std::cin >> n;
const int x = n;x is const, but its value is only known at runtime.
5.2. Using runtime values in constexpr variables
int n;
std::cin >> n;
constexpr int x = n; // ErrorA constexpr variable must be initialized with a compile-time value.
5.3. Writing complex C++11 constexpr functions
C++11 constexpr functions are limited.
They should be simple and usually contain one return statement.
Phần 6
6. When Should You Use constexpr?
Use constexpr when:
1. The value should be known at compile time.
2. You define constants such as sizes, limits, or fixed parameters.
3. You write small pure functions for compile-time calculation.
4. You want safer alternatives to macros.Avoid it when:
1. The value depends on user input.
2. The value depends on runtime data.
3. The logic is too complex for C++11 constexpr.Phần 7
7. Notes for Trading / Performance-Sensitive Code
Trading systems often use fixed constants:
Maximum orders
Price scale
Lot size
Tick size
Risk limits
Array sizesExample:
constexpr int MaxOrders = 1000;
constexpr double TickSize = 0.01;constexpr makes these constants explicit and available at compile time.
Phần 8
8. End-of-Day Checklist
You should remember:
constexpr = can be evaluated at compile timeYou should be able to answer:
1. What does constexpr mean?
2. What is the difference between const and constexpr?
3. Can constexpr depend on user input?
4. Why is constexpr better than macro constants?
5. When is a constexpr function useful?Phần 9
9. Conclusion
constexpr is used for values and functions that can be evaluated at compile time.
The key idea:
Use constexpr for true compile-time constants.
Use const when you only need immutability.:::
Mã mẫu hoàn chỉnh
cpp11/12_constexpr/main.cpp
#include <iostream>
constexpr int MaxOrders = 1000;
constexpr int doubleValue(int x) {
return x * 2;
}
int main() {
int orderIds[MaxOrders];
constexpr int Limit = doubleValue(50);
orderIds[0] = 101;
std::cout << "Max orders = " << MaxOrders << '\n';
std::cout << "Limit = " << Limit << '\n';
std::cout << "First order id = " << orderIds[0] << '\n';
return 0;
}Tự kiểm tra · không chấm điểm
Bạn đã hiểu những điểm nào?
Đã tự kiểm tra 0/5 mục