From 8eea8b70aa0b840ef1765bc359adc0b18cc3e763 Mon Sep 17 00:00:00 2001 From: Wang Zi Yan Date: Thu, 1 Jun 2023 12:10:47 +0800 Subject: [PATCH] Docs: Update CN for /api-reference/api-conventions.rst --- docs/en/api-reference/api-conventions.rst | 4 ++-- docs/zh_CN/api-reference/api-conventions.rst | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/en/api-reference/api-conventions.rst b/docs/en/api-reference/api-conventions.rst index f90688ed05..710e0e2a9c 100644 --- a/docs/en/api-reference/api-conventions.rst +++ b/docs/en/api-reference/api-conventions.rst @@ -55,7 +55,7 @@ Most ESP-IDF examples use C99 `designated initializers`_ for structure initializ /* Correct, fields .arg and .name are zero-initialized */ }; -The C++ language supports designated initializers syntax, too, but the initializers must be in the order of declaration. When using ESP-IDF APIs in C++ code, you may consider using the following pattern:: +The C++ language supports designated initializer syntax, too, but the initializers must be in the order of declaration. When using ESP-IDF APIs in C++ code, you may consider using the following pattern:: /* Correct, fields .dispatch_method, .name and .skip_unhandled_events are zero-initialized */ const esp_timer_create_args_t my_timer_args = { @@ -69,7 +69,7 @@ The C++ language supports designated initializers syntax, too, but the initializ // .callback = &my_timer_callback, //}; -For more information on designated initializers, see :ref:`Designated initializers `. Note that C++ language versions older than C++20 (not the default in the current version of ESP-IDF) do not support designated initializers. If you have to compile code with an older C++ standard than C++20, you may use GCC extensions to produce the following pattern:: +For more information on designated initializers, see :ref:`cplusplus_designated_initializers`. Note that C++ language versions older than C++20, which are not the default in the current version of ESP-IDF, do not support designated initializers. If you have to compile code with an older C++ standard than C++20, you may use GCC extensions to produce the following pattern:: esp_timer_create_args_t my_timer_args = {}; /* All the fields are zero-initialized */ diff --git a/docs/zh_CN/api-reference/api-conventions.rst b/docs/zh_CN/api-reference/api-conventions.rst index d2214f5f6f..51a99bb690 100644 --- a/docs/zh_CN/api-reference/api-conventions.rst +++ b/docs/zh_CN/api-reference/api-conventions.rst @@ -55,10 +55,24 @@ ESP-IDF 由多个组件组成,组件中包含专门为 ESP 芯片编写的代 /* 正确,字段 .arg 和 .name 已初始化为零 */ }; -C++ 语言在 C++20 之前不支持指定初始化器语法,但是 GCC 编译器可以作为扩展实现部分指定初始化器功能。在 C++ 代码中使用 ESP-IDF API 时,可以考虑使用以下模式:: +C++ 语言同样支持指定初始化器语法,但初始化器必须遵循声明顺序。在 C++ 代码中使用 ESP-IDF API 时,可以考虑使用以下模式:: + + /* 正确:.dispatch_method、.name 以及 .skip_unhandled_events 初始化为零 */ + const esp_timer_create_args_t my_timer_args = { + .callback = &my_timer_callback, + .arg = &my_arg, + }; + + ///* 错误:esp_timer_create_args_t 中,.arg 在 .callback 之后声明 */ + //const esp_timer_create_args_t my_timer_args = { + // .arg = &my_arg, + // .callback = &my_timer_callback, + //}; + +了解指定初始化器的更多信息,请参见 :ref:`cplusplus_designated_initializers`。注意,C++20 之前的 C++ 语言不是当前 ESP-IDF 的默认版本,不支持指定初始化器。如需使用 C++20 之前的 C++ 标准编译代码,可以借助 GCC 扩展生成以下模式:: esp_timer_create_args_t my_timer_args = {}; - /* 所有字段已初始化为零 */ + /* 所有字段初始化为零 */ my_timer_args.callback = &my_timer_callback; 默认初始化器