Skip to content

Commit 2372cb4

Browse files
committed
Modified the error description in the constexpr section.changkun#289
1 parent af45678 commit 2372cb4

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

book/en-us/02-usability.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ we need to use the `constexpr` feature introduced in C++11, which will be introd
147147
to solve this problem; for `arr_5`, before C++98 The compiler cannot know that `len_foo()`
148148
actually returns a constant at runtime, which causes illegal production.
149149
150-
> Note that most compilers now have their compiler optimizations.
151-
> Many illegal behaviors become legal under the compiler's optimization.
152-
> If you need to reproduce the error, you need to use the old version of the compiler.
150+
> Note that some compilers (e.g. GCC, Clang) have compiler extensions enabled by default, supporting a C feature called "[variable-length arrays](https://en.cppreference.com/w/c/language/array#.E9.9D.9E.E5.B8.B8.E9.87.8F.E9.95.BF.E5.BA.A6.E6.95.B0.E7.BB.84)", which allows you to define an array whose length can be a non-constant expression. Code that causes the above comments to be illegal can be compiled. To disable the extension, add the compilation option [`-pedantic-errors`](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-pedantic-errors) (both GCC and Clang are available).
153151
154152
C++11 provides `constexpr` to let the user explicitly declare that the function or
155153
object constructor will become a constant expression at compile time.

book/zh-cn/02-usability.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ int main() {
119119
120120
上面的例子中,`char arr_4[len_2]` 可能比较令人困惑,因为 `len_2` 已经被定义为了常量。为什么 `char arr_4[len_2]` 仍然是非法的呢?这是因为 C++ 标准中数组的长度必须是一个常量表达式,而对于 `len_2` 而言,这是一个 `const` 常数,而不是一个常量表达式,因此(即便这种行为在大部分编译器中都支持,但是)它是一个非法的行为,我们需要使用接下来即将介绍的 C++11 引入的 `constexpr` 特性来解决这个问题;而对于 `arr_5` 来说,C++98 之前的编译器无法得知 `len_foo()` 在运行期实际上是返回一个常数,这也就导致了非法的产生。
121121
122-
> 注意,现在大部分编译器其实都带有自身编译优化,很多非法行为在编译器优化的加持下会变得合法,若需重现编译报错的现象需要使用老版本的编译器
122+
> 注意,一些编译器(如 GCC、Clang)默认开启了编译器扩展,支持了 C 语言的特性:“[变长数组](https://zh.cppreference.com/w/c/language/array#.E9.9D.9E.E5.B8.B8.E9.87.8F.E9.95.BF.E5.BA.A6.E6.95.B0.E7.BB.84)”,允许定义数组时,其长度的表达式可以是非常量表达式。导致以上注释了非法的代码可以通过编译。想要禁用扩展可以添加编译选项 [`-pedantic-errors`](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-pedantic-errors)(GCC 与 Clang 都可用)
123123
124124
C++11 提供了 `constexpr` 让用户显式的声明函数或对象构造函数在编译期会成为常量表达式,这个关键字明确的告诉编译器应该去验证 `len_foo` 在编译期就应该是一个常量表达式。
125125

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy