首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

attribute specifier sequence(since C++11)

介绍类型、对象、代码等的实现定义属性。[[

ATTR]][[吸引1,吸引2,吸引3(阿格斯)]][[命名空间::ATTR(阿格斯)]]对齐[医]说明符

形式上,语法是。

[ attribute-list ]

?

(since C++11)

[ using attribute-namespace : attribute-list ]

?

(since C++17)

其中,属性列表是以省略号结尾的逗号分隔的零或多个属性%28的序列。...指示包装膨胀29%。

identifier

?

?

attribute-namespace :: identifier

?

?

identifier ( argument-list )

?

?

attribute-namespace :: identifier ( argument-list )

?

?

1%29简单属性,如[[noreturn]]

带有命名空间的2%29属性,如[[gnu::unused]]

带有参数的3%29属性,如[[deprecated("because")]]

具有命名空间和参数列表的4%29属性

If using: namespace appears in the beginning of an attribute list, no other attributes in the attribute list can specify a namespace: the namespace specified in a using applies to them all: [using CC: opt(1), debug] // same as [CC::opt(1), CC::debug] [using CC: CC::opt(1)] // error: cannot combine using and scoped attribute

(since C++17)

解释

属性为实现定义的语言扩展提供统一的标准语法,例如gng和ibm语言扩展。__attribute__((...)),Microsoft扩展__declspec()

属性几乎可以在C++程序中的任何地方使用,并且几乎可以应用于所有东西:类型、变量、函数、名称、代码块、整个转换单元,尽管每个特定属性只有在实现允许的情况下才有效:[[likely]]可以是只能与if,而不是用类声明。[[omp::parallel()]]可以是应用于代码块或for循环,但不是类型。int这两个属性都是虚构的示例,请参阅下面的标准和一些非标准属性%29。

在声明中,属性可能出现在声明的实体名称之前和之后,在这种情况下,它们是组合的。在大多数其他情况下,属性适用于直接前面的实体。

对齐[医]说明符是属性说明符序列的一部分,尽管它有不同的语法。它可能出现在[[...]]属性将出现,并且可以与它们混合使用%28,条件是在以下位置使用它:alignas允许%29。

连续两个左方括号令牌%28[[%5月29日仅在引入属性说明符或在属性参数中出现时才出现。

二次

代码语言:javascript
复制
void f() {
  int y[3];
  y[[] { return 0; }()] = 1;    // error
  int i [[cats::meow([[]])]]; // OK
}

二次

除了下面列出的标准属性之外,实现还可以支持具有实现定义行为的任意非标准属性。所有未知的属性都会被忽略,而不会导致错误。%28自C++17%29。

标准属性

C++标准只定义了以下属性。

[noreturn]

Indicates that the function does not return. This attribute applies to function declarations only. The behavior is undefined if the function with this attribute actually returns. The following standard functions have this attribute: std::_Exit, std::abort, std::exit, std::quick_exit, std::unexpected, std::terminate, std::rethrow_exception, std::throw_with_nested, std::nested_exception::rethrow_nested

[carries_dependency]

Indicates that dependency chain in release-consume std::memory_order propagates in and out of the function, which allows the compiler to skip unnecessary memory fence instructions.This attribute may appear in two situations: 1) it may apply to the parameter declarations of a function or lambda-expressions, in which case it indicates that initialization of the parameter carries dependency into lvalue-to-rvalue conversion of that object. 2) It may apply to the function declaration as a whole, in which case it indicates that the return value carries dependency to the evaluation of the function call expression. This attribute must appear on the first declaration of a function or one of its parameters in any translation unit. If it is not used on the first declaration of a function or one of its parameters in another translation unit, the program is ill-formed; no diagnostic required. See std::kill_dependency for example usage.

[deprecated][deprecated("reason")]

Indicates that the use of the name or entity declared with this attribute is allowed, but discouraged for some reason. This attribute is allowed in declarations of classes, typedef-names, variables, non-static data members, functions, enumerations, and template specializations. A name declared non-deprecated may be redeclared deprecated. A name declared deprecated cannot be un-deprecated by redeclaring it without this attribute.

[fallthrough]

Appears in a switch statement on a line of its own (technically as an attribute of a null statement), immediately before a case label. Indicates that the fall through from the previous case label is intentional and should not be diagnosed by a compiler that warns on fallthrough.

[nodiscard]

Appears in a function declaration, enumeration declaration, or class declaration. If a function declared nodiscard or a function returning an enumeration or class declared nodiscard by value is called from a discarded-value expression other than a cast to void, the compiler is encouraged to issue a warning.

[maybe_unused]

Appears in the declaration of a class, a typedef?, a variable, a non?static data member, a function, an enumeration, or an enumerator. If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared maybe_unused.

[optimize_for_synchronized]

Appears on a declarator of a function declaration, which must be the first declaration of the function. indicates that the function definition should be optimized for invocation from a synchronized statement. In particular, it avoids serializing synchronized blocks that make a call to a function that is transaction-safe for the majority of calls, but not for all calls.

二次

代码语言:javascript
复制
#include <cassert>
[[ noreturn ]] void f() {
  throw "error";
  // OK
}
 
[[ noreturn ]] void q(int i) {
  // behavior is undefined if called with an argument <= 0
  if (i > 0) {
    throw "positive";
  }
}
 
void f(int n) {
  void g(), h(), i();
  switch (n) {
    case 1:
    case 2:
      g();
     [[fallthrough]];
    case 3: // no warning on fallthrough
      h();
    case 4: // compiler may warn on fallthrough
      i();
      [[fallthrough]]; // ill?formed, not before a case label
  }
}
 
struct [[nodiscard]] error_info { };
error_info enable_missile_safety_mode();
void launch_missiles();
void test_missiles() {
   enable_missile_safety_mode(); // compiler may warn on discarding a nodiscard value
   launch_missiles();
}
error_info& foo();
void f1() {
    foo(); // nodiscard type is not returned by value, no warning
} 
 
 
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
                        [[maybe_unused]] bool thing2)
{
   [[maybe_unused]] bool b = thing1 && thing2;
   assert(b); // in release mode, assert is compiled out, and b is unused
              // no warning because it is declared [[maybe_unused]]
} // parameters thing1 and thing2 are not used, no warning

二次

外部链接

另见

c文件[医]回绝

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com