cppreference.com 文档中的“(可选)”标记
The "(optional)" marker in cppreference.com documentation
上周,我与一位同事就 cppreference.com. We had a look at the documentation of the parameter packs 上的 C++ 功能文档进行了讨论,特别是 (optional)
标记的含义:
(可以找到另一个示例 here。)
我认为这意味着这部分语法是可选的。这意味着我可以在语法中省略这部分,但它始终需要编译器支持以符合 C++ 标准。但他表示,这意味着它在标准中是可选的,编译器不需要支持这个特性来符合标准。是哪个?这两种解释对我来说都很有意义。
我在 cppreference 网站上找不到任何类型的解释。我也尝试 google 它但总是降落在 std::optional
...
这意味着特定的令牌是可选的。例如,这两个声明都有效:
template <class... Args>
void foo();
template <class...>
void bar();
虽然我找到了一个列出所有标记的页面,但我找不到指定标记含义的页面。不过,我可能会请您的同事看一下其他页面,目的是让同事放弃“可选”意味着“可选择支持”的想法。 (这不是一个明确的论点,但很多人会觉得它有说服力。)我在 Function declaration.
找到了两个很好的例子
Function declaration:
noptr-declarator ( parameter-list ) cv(optional) ref(optional) except(optional) attr(optional)
关注 cv
(“const-volatile”的缩写),它被标记为“可选”并且“只允许在非静态成员函数声明中使用”。您的同事对该标记的解释意味着编译器不必支持 const
成员函数,因为 const
关键字是“可选的”。
Function definition,第一个选项为function-body
:
ctor-initializer(optional) compound-statement
这里的“可选”部分是成员初始化列表(只允许在构造函数中使用)。您的同事准备好声称编译器不需要支持成员初始化列表了吗?
有时候应该看看熟悉的注解才能理解。
opt / (optional)后缀表示该符号是可选的[供C++程序员使用;编译器不支持]
由于这个问题已被标记为语言律师,并且当我们寻找明确的参考时,我们通常会远离 CppReference 和 into the standard。
其中CppReference使用(可选)下标,标准使用opt;例如如 [temp.param]/1:
The syntax for template-parameters is:
- template-parameter:
- type-parameter
- parameter-declaration
- type-parameter:
- type-parameter-key ...opt identifieropt
- [... and so on]
[syntax]/1描述语法符号[强调我的]:
In the syntax notation used in this document, syntactic categories are
indicated by italic type, and literal words and characters in constant
width type. Alternatives are listed on separate lines except in a few
cases where a long set of alternatives is marked by the phrase “one
of”. If the text of an alternative is too long to fit on a line, the
text is continued on subsequent lines indented from the first one.
An optional terminal or non-terminal symbol is indicated by the subscript “opt", so
- { expressionopt }
indicates an optional expression enclosed in braces.
所以,你是对的,你同事是错的。特别是对于模板参数包的示例(我们通过 typename
之后的可选 ...
引入)typename...
之后的 identifier
,它命名包(或模板)参数,如果 ...
被省略)是可选的。
But he stated that it means that it is optional in the standard and that a compiler does not need to support this feature to comply to the standard.
如果我们用单一类型模板参数注释 class 模板的“可选排列”,这种说法的荒谬就变得更加明显:
template<typename>
// ^^^^^^^^ type-parameter
// (omitting optional '...' and 'identifier')
struct S;
template<typename T>
// ^^^^^^^^^^ type-parameter
// (omitting optional '...')
struct S;
template<typename...>
// ^^^^^^^^^^^ type-parameter
// (omitting optional 'identifier')
struct S;
template<typename... Ts>
// ^^^^^^^^^^^^^^ type-parameter
struct S;
如果上面的说法是正确的,那么只有这四个中的第一个 需要 由兼容的实现支持(在这个人为的示例中仅基于语法),它这意味着编译器供应商可以提供一个兼容的实现,我们永远无法命名模板(类型)参数或函数参数。
上周,我与一位同事就 cppreference.com. We had a look at the documentation of the parameter packs 上的 C++ 功能文档进行了讨论,特别是 (optional)
标记的含义:
(可以找到另一个示例 here。)
我认为这意味着这部分语法是可选的。这意味着我可以在语法中省略这部分,但它始终需要编译器支持以符合 C++ 标准。但他表示,这意味着它在标准中是可选的,编译器不需要支持这个特性来符合标准。是哪个?这两种解释对我来说都很有意义。
我在 cppreference 网站上找不到任何类型的解释。我也尝试 google 它但总是降落在 std::optional
...
这意味着特定的令牌是可选的。例如,这两个声明都有效:
template <class... Args>
void foo();
template <class...>
void bar();
虽然我找到了一个列出所有标记的页面,但我找不到指定标记含义的页面。不过,我可能会请您的同事看一下其他页面,目的是让同事放弃“可选”意味着“可选择支持”的想法。 (这不是一个明确的论点,但很多人会觉得它有说服力。)我在 Function declaration.
找到了两个很好的例子Function declaration:
noptr-declarator ( parameter-list ) cv(optional) ref(optional) except(optional) attr(optional)
关注cv
(“const-volatile”的缩写),它被标记为“可选”并且“只允许在非静态成员函数声明中使用”。您的同事对该标记的解释意味着编译器不必支持const
成员函数,因为const
关键字是“可选的”。Function definition,第一个选项为
function-body
:
ctor-initializer(optional) compound-statement
这里的“可选”部分是成员初始化列表(只允许在构造函数中使用)。您的同事准备好声称编译器不需要支持成员初始化列表了吗?
有时候应该看看熟悉的注解才能理解。
opt / (optional)后缀表示该符号是可选的[供C++程序员使用;编译器不支持]
由于这个问题已被标记为语言律师,并且当我们寻找明确的参考时,我们通常会远离 CppReference 和 into the standard。
其中CppReference使用(可选)下标,标准使用opt;例如如 [temp.param]/1:
The syntax for template-parameters is:
- template-parameter:
- type-parameter
- parameter-declaration
- type-parameter:
- type-parameter-key ...opt identifieropt
- [... and so on]
[syntax]/1描述语法符号[强调我的]:
In the syntax notation used in this document, syntactic categories are indicated by italic type, and literal words and characters in constant width type. Alternatives are listed on separate lines except in a few cases where a long set of alternatives is marked by the phrase “one of”. If the text of an alternative is too long to fit on a line, the text is continued on subsequent lines indented from the first one. An optional terminal or non-terminal symbol is indicated by the subscript “opt", so
- { expressionopt }
indicates an optional expression enclosed in braces.
所以,你是对的,你同事是错的。特别是对于模板参数包的示例(我们通过 typename
之后的可选 ...
引入)typename...
之后的 identifier
,它命名包(或模板)参数,如果 ...
被省略)是可选的。
But he stated that it means that it is optional in the standard and that a compiler does not need to support this feature to comply to the standard.
如果我们用单一类型模板参数注释 class 模板的“可选排列”,这种说法的荒谬就变得更加明显:
template<typename>
// ^^^^^^^^ type-parameter
// (omitting optional '...' and 'identifier')
struct S;
template<typename T>
// ^^^^^^^^^^ type-parameter
// (omitting optional '...')
struct S;
template<typename...>
// ^^^^^^^^^^^ type-parameter
// (omitting optional 'identifier')
struct S;
template<typename... Ts>
// ^^^^^^^^^^^^^^ type-parameter
struct S;
如果上面的说法是正确的,那么只有这四个中的第一个 需要 由兼容的实现支持(在这个人为的示例中仅基于语法),它这意味着编译器供应商可以提供一个兼容的实现,我们永远无法命名模板(类型)参数或函数参数。