const and constexpr
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A C++ program lives twice: once while the compiler builds it, and again every time it runs. constexpr asks the compiler to work a value out while it builds, so it is baked into the program and can size an array. const only promises that a value never changes after it is set, and that value may come from run time. Follow a ten-line program as sq(4) is computed by the compiler, argc arrives when the program runs, and two small mistakes stop the build.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01In constexpr int N = sq(4);, when is sq(4) worked out?
A constexpr variable must be initialized by a constant expression, so the compiler evaluates sq(4) during the build and bakes 16 into the program.
02Which line compiles?
argc is only known when the program runs. const accepts a run-time value and just forbids later changes. constexpr, array sizes and static_assert all need a value known at compile time.
03const int k = argc * 3; and later k = 5;. What happens?
Writing to a const object is caught by the compiler (GCC: assignment of read-only variable 'k'), so no program is made.
04sq is declared constexpr. What happens in return sq(k); when k comes from argc?
constexpr on a function means it may be evaluated at compile time. With a run-time argument it simply runs like any other function. A consteval function (C++20) would refuse.
05Which statement about const and constexpr is true?
A constexpr variable is implicitly const. The difference is when the value is known: constexpr requires compile time; const allows run time.
المسرد
- Compile time
- While the compiler is building your program from source code. Nothing has run yet.
- Run time
- While the built program is running on the CPU. Values such as argc or user input only exist now.
- const
- A promise that an object is read-only after it is initialized. Its value may be worked out at run time.
- constexpr variable
- A variable whose value the compiler must work out while building. It is also const.
- constexpr function
- A function the compiler may run at compile time when its arguments are constant; otherwise it runs normally at run time.
- Constant expression
- An expression the compiler can evaluate while building, needed for array sizes, static_assert and template arguments.
- static_assert
- A check the compiler makes at compile time. If its condition is false, the build stops.
- consteval
- C++20: marks a function that must run at compile time; calling it with a run-time value is an error.
الملاحظات والمصادر
- Compile time and run time: the compiler (g++ or clang++) turns app.cpp into the program file app; running ./app happens later, as often as you like. Values a program reads when it runs, such as argc or user input, cannot be known while it is built.
- The standard guarantees that N's value is known to the compiler, so it can be used where a constant expression is required. Whether N also gets a slot in memory is up to the compiler; in practice 16 is written into the machine code wherever N is used.
- A const int initialized from a constant, such as const int L = 16;, is also usable in constant expressions. constexpr makes the requirement explicit and checked: if the value cannot be computed at compile time, the build fails.
- GCC and Clang accept int a[n] with a run-time n as an extension (a variable-length array, borrowed from C); standard C++ does not allow it, and -Wvla or -pedantic warns about it.
- Exact messages differ: for the write to k, GCC says "assignment of read-only variable 'k'" and Clang says "cannot assign to variable 'k' with const-qualified type 'const int'". For sq(argc), Clang says "constexpr variable 'M' must be initialized by a constant expression"; GCC also rejects it, in its own words.
- The program on the card is real, standard C++20. It compiles with no warnings under g++ or clang++ -std=c++20 -Wall -Wextra; ./app exits with status 9 and ./app a b with 81.
- cppreference.com: constexpr specifier
- cppreference.com: Constant expressions
- cppreference.com: cv (const and volatile) type qualifiers
- cppreference.com: static_assert declaration
- cppreference.com: consteval specifier
- cppreference.com: Array declaration
- cppreference.com: Main function
- isocpp.org C++11 FAQ: constexpr
- isocpp.org FAQ: Const correctness
- C++ Core Guidelines: Constants and immutability (Con.1 to Con.5)
اختبار يتاح بعد الخطوة الأخيرة.