简体   繁体   English

function主要参数的属性

[英]Attributes for main function parameters

Can I use attributes for main function parameters or is it implementation defined?我可以使用主要 function 参数的属性还是它是实现定义的?

Looks like main function has only 2 supported forms without attribute-list while the general function declaration syntax does have it.看起来主要的 function 只有 2 个支持的 forms 没有属性列表,而一般的function 声明语法确实有它。

Example:例子:

int main([[maybe_unused]] int argc, char* argv[]);

Indeed there is no explicit requirement that attributes must be accepted for main function parameters basic.start.main .实际上,没有明确要求必须接受主 function 参数basic.start.main的属性。

But on the other hand if you read dcl.attr.unused#5 you can't find anything special for main which says that is not allowed there.但另一方面,如果您阅读dcl.attr.unused#5 ,您将找不到任何特别的 main 内容,这表明那里不允许这样做。

This attribute must be known by a compiler to be C++17 conformant, but even unknown attributes should not cause errors.编译器必须知道此属性与 C++17 一致,但即使是未知属性也不应该导致错误。 You can find this in the standard:你可以在标准中找到这个:

Any attribute-token that is not recognized by the implementation is ignored.任何不被实现识别的属性标记都会被忽略。 dcl.attr#grammar-6 dcl.attr#grammar-6

Unfortunately attributes can cause sometimes errors (even if they shouldn't).不幸的是,属性有时会导致错误(即使它们不应该)。 See for expamle this issue: GSL_SUPPRESS .请参阅此问题的示例: GSL_SUPPRESS

In practice your code is accepted by all major compilers without a warning Godbolt .在实践中,所有主要编译器都接受您的代码,而不会发出警告Godbolt Therefore I would say it is okay.所以我会说没关系。 But because it is allowed to have a main function which takes no arguments I would prefer that.但是因为它允许有一个主要的 function 没有 arguments 我更喜欢那个。

Can I use attributes for main function parameters or is it implementation defined?我可以使用主要 function 参数的属性还是它是实现定义的?

From dcl.attr.grammar :dcl.attr.grammar

For an attribute-token (including an attribute-scoped-token) not specified in this document, the behavior is implementation-defined.对于本文档中未指定的属性令牌(包括属性范围令牌),其行为是实现定义的。

Since the attribute appertains to the parameter, and that affects the declaration of main , the behavior of such a program is implementation-defined, and is not portable across conforming implementations.由于属性属于参数,并且会影响main的声明,因此此类程序的行为是实现定义的,并且不能跨一致的实现移植。

For your example of [[maybe_unused]] , this attribute is specified in dcl.attr.unused .对于您的[[maybe_unused]]示例,此属性在dcl.attr.unused中指定。 There appears to be no wording that this attribute affects the type of a variable declaration, or has any other semantic effect on the behavior of the program, so this program is portable.似乎没有任何措辞表明该属性会影响变量声明的类型,或者对程序的行为有任何其他语义影响,因此该程序是可移植的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM