Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Attribute specifier sequence(since C++11)

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Declarations
       
      Attributes
       

      Introduces implementation-defined attributes for types, objects, code, etc.

      Contents

      [edit]Syntax

      [[attribute-list]](since C++11)
      [[usingattribute-namespace:attribute-list]](since C++17)

      whereattribute-list is a comma-separated sequence of zero or moreattribute s (possibly ending with an ellipsis... indicating apack expansion)

      identifier (1)
      attribute-namespace::identifier (2)
      identifier(argument-list (optional)) (3)
      attribute-namespace::identifier(argument-list (optional)) (4)

      whereattribute-namespace is anidentifier andargument-list is a sequence of tokens where parentheses, brackets and braces are balanced (balanced-token-seq).

      1) Simple attribute, such as[[noreturn]].
      2) Attribute with a namespace, such as[[gnu::unused]].
      3) Attribute with arguments, such as[[deprecated("because")]].
      4) Attribute with both a namespace and an argument list.

      Ifusing 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)

      [edit]Explanation

      Attributes provide the unified standard syntax for implementation-defined language extensions, such as the GNU and IBM language extensions__attribute__((...)), Microsoft extension__declspec(), etc.

      An attribute can be used almost everywhere in the C++ program, and can be applied to almost everything: to types, to variables, to functions, to names, to code blocks, to entire translation units, although each particular attribute is only valid where it is permitted by the implementation:[[expect_true]] could be an attribute that can only be used with anif, and not with a class declaration.[[omp::parallel()]] could be an attribute that applies to a code block or to afor loop, but not to the typeint, etc (note these two attributes are fictional examples, see below for the standard and some non-standard attributes).

      In declarations, attributes may appear both before the whole declaration and directly after the name of the entity that is declared, in which case they are combined. In most other situations, attributes apply to the directly preceding entity.

      Thealignas specifier is a part of the attribute specifier sequence, although it has different syntax. It may appear where the[[...]] attributes appear and may mix with them (provided it is used wherealignas is permitted).

      Two consecutive left square bracket tokens ([[) may only appear when introducing an attribute-specifier or inside an attribute argument.

      void f(){int y[3];    y[[]{return0;}()]=1;// errorint i[[cats::meow([[]])]];// OK}

      Besides the standard attributes listed below, implementations may support arbitrary non-standard attributes with implementation-defined behavior.All attributes unknown to an implementation are ignored without causing an error.(since C++17)

      An attribute withoutattribute-namespace and anattribute-namespace whose name is eitherstd orstd followed by one or more digits is reserved for future standardization. That is, every non-standard attribute is in theattribute-namespace provided by the implementation, e.g.[[gnu::may_alias]],[[clang::trivial_abi]], and[[msvc::noop_dtor]].

      (since C++20)

      [edit]Standard attributes

      The following attributes are defined by the C++ standard.

      Standard attributes cannot be syntactically ignored: they cannot contain syntax errors, must be applied to the correct target, and entities in the arguments must beODR-use.

      Standard attributes cannot be semantically ignored either: the behavior with all instances of a particular standard attribute removed would have been a conforming behavior for the original program with the attribute present.

      (C++11)
      indicates that the function does not return
      (attribute specifier)[edit]
      (C++11)(removed in C++26)
      indicates that dependency chain in release-consumestd::memory_order propagates in and out of the function
      (attribute specifier)[edit]
      [[deprecated]][[deprecated("reason")]]
      (C++14)(C++14)
      indicates that the use of the name or entity declared with this attribute is allowed, but discouraged for somereason
      (attribute specifier)[edit]
      (C++17)
      indicates that the fall through from the previous case label is intentional and should not be diagnosed by a compiler that warns on fall-through
      (attribute specifier)[edit]
      (C++17)
      suppresses compiler warnings on unused entities, if any
      (attribute specifier)[edit]
      [[nodiscard]][[nodiscard("reason")]]
      (C++17)(C++20)
      encourages the compiler to issue a warning if the return value is discarded
      (attribute specifier)[edit]
      (C++20)(C++20)
      indicates that the compiler should optimize for the case where a path of execution through a statement is more or less likely than any other path of execution
      (attribute specifier)[edit]
      indicates that a non-static data member need not have an address distinct from all other non-static data members of its class
      (attribute specifier)[edit]
      [[assume(expression)]]
      (C++23)
      specifies that theexpression will always evaluate totrue at a given point
      (attribute specifier)[edit]
      (C++26)
      specifies that an object has an indeterminate value if it is not initialized
      (attribute specifier)[edit]
      indicates that the function definition should be optimized for invocation from asynchronized statement
      (attribute specifier)[edit]

      [edit]Notes

      The presence of each individual attribute on a given platform can be checked with__has_cpp_attribute preprocessor macro.

      Feature-test macroValueStdFeature
      __cpp_attributes200809L(C++11)Attributes
      __cpp_namespace_attributes201411L(C++17)Attributes fornamespaces

      [edit]Example

      Run this code
      [[gnu::always_inline]][[gnu::hot]][[gnu::const]][[nodiscard]]inlineint f();// declare f with four attributes [[gnu::always_inline, gnu::const, gnu::hot, nodiscard]]int f();// same as above, but uses a single attr specifier that contains four attributes // C++17:[[using gnu:const, always_inline, hot]][[nodiscard]]int f[[gnu::always_inline]]();// an attribute may appear in multiple specifiers int f(){return0;} int main(){}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 2079C++11[[ could not appear inside an attribute argumentallowed
      CWG 2538C++11it was unclear whether standard attributes can be syntactically ignoredprohibited
      CWG 2695C++11it was unclear whether standard attributes can be semantically ignoredprohibited
      P2156R1C++11every standard attribute was required to appear at most once in anattribute-listnot required

      [edit]See also

      __has_cpp_attribute - checks for the presence of an attribute
      C documentation forAttributes specifier sequence

      [edit]External links

      1. Attributes in GCC. These attributes can be used as[[gnu::...]],See SO.
      2. Attributes in Clang.
      3. Attributes in MSVC.
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/attributes&oldid=179437"

      [8]ページ先頭

      ©2009-2025 Movatter.jp