Files
2025-10-25 03:02:53 +03:00

214 lines
16 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[dcl.attr.grammar]
# 9 Declarations [[dcl]](./#dcl)
## 9.13 Attributes [[dcl.attr]](dcl.attr#grammar)
### 9.13.1 Attribute syntax and semantics [dcl.attr.grammar]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9349)
Attributes and annotations specify additional information for various source constructs
such as types, variables, names, contract assertions, blocks, or translation units[.](#1.sentence-1)
[attribute-specifier-seq:](#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[*attribute-specifier*](#nt:attribute-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") [*attribute-specifier-seq*](#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt
[attribute-specifier:](#nt:attribute-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[ [ [*attribute-using-prefix*](#nt:attribute-using-prefix "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt [*attribute-list*](#nt:attribute-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") ] ]
[ [ [*annotation-list*](#nt:annotation-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") ] ]
[*alignment-specifier*](#nt:alignment-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[alignment-specifier:](#nt:alignment-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
alignas ( [*type-id*](dcl.name#nt:type-id "9.3.2Type names[dcl.name]") ...opt )
alignas ( [*constant-expression*](expr.const#nt:constant-expression "7.7Constant expressions[expr.const]") ...opt )
[attribute-using-prefix:](#nt:attribute-using-prefix "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
using [*attribute-namespace*](#nt:attribute-namespace "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") :
[attribute-list:](#nt:attribute-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt
[*attribute-list*](#nt:attribute-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") , [*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt
[*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") ...
[*attribute-list*](#nt:attribute-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") , [*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") ...
[annotation-list:](#nt:annotation-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[*annotation*](#nt:annotation "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") ...opt
[*annotation-list*](#nt:annotation-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") , [*annotation*](#nt:annotation "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") ...opt
[attribute:](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[*attribute-token*](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") [*attribute-argument-clause*](#nt:attribute-argument-clause "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt
[annotation:](#nt:annotation "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
= [*constant-expression*](expr.const#nt:constant-expression "7.7Constant expressions[expr.const]")
[attribute-token:](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]")
[*attribute-scoped-token*](#nt:attribute-scoped-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[attribute-scoped-token:](#nt:attribute-scoped-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[*attribute-namespace*](#nt:attribute-namespace "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") :: [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]")
[attribute-namespace:](#nt:attribute-namespace "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]")
[attribute-argument-clause:](#nt:attribute-argument-clause "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
( [*balanced-token-seq*](#nt:balanced-token-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt )
[balanced-token-seq:](#nt:balanced-token-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
[*balanced-token*](#nt:balanced-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") [*balanced-token-seq*](#nt:balanced-token-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt
[balanced-token:](#nt:balanced-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")
( [*balanced-token-seq*](#nt:balanced-token-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt )
[ [*balanced-token-seq*](#nt:balanced-token-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt ]
{ [*balanced-token-seq*](#nt:balanced-token-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt }
[: [*balanced-token-seq*](#nt:balanced-token-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt :]
any [*token*](lex.token#nt:token "5.10Tokens[lex.token]") other than (, ), [, ], {, }, [:, or :]
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9437)
If an [*attribute-specifier*](#nt:attribute-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") contains an [*attribute-using-prefix*](#nt:attribute-using-prefix "9.13.1Attribute syntax and semantics[dcl.attr.grammar]"),
the [*attribute-list*](#nt:attribute-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") following that [*attribute-using-prefix*](#nt:attribute-using-prefix "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") shall not contain an [*attribute-scoped-token*](#nt:attribute-scoped-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") and every [*attribute-token*](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") in that [*attribute-list*](#nt:attribute-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") is treated as if
its [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]") were prefixed with N::,
where N is the [*attribute-namespace*](#nt:attribute-namespace "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") specified in the [*attribute-using-prefix*](#nt:attribute-using-prefix "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")[.](#2.sentence-1)
[*Note [1](#note-1)*:
This rule imposes no constraints on how
an [*attribute-using-prefix*](#nt:attribute-using-prefix "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") affects the tokens in an [*attribute-argument-clause*](#nt:attribute-argument-clause "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")[.](#2.sentence-2)
— *end note*]
[*Example [1](#example-1)*: [[using CC: opt(1), debug]] // same as [[CC::opt(1), CC::debug]]void f() {}[[using CC: opt(1)]] [[CC::debug]] // same as [[CC::opt(1)]] [[CC::debug]]void g() {}[[using CC: CC::opt(1)]] // error: cannot combine using and scoped attribute tokenvoid h() {} — *end example*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9463)
[*Note [2](#note-2)*:
For each individual attribute, the form of the[*balanced-token-seq*](#nt:balanced-token-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") will be specified[.](#3.sentence-1)
— *end note*]
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9469)
In an [*attribute-list*](#nt:attribute-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]"), an ellipsis may appear only if that[*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")'s specification permits it[.](#4.sentence-1)
An [*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") followed
by an ellipsis is a [pack expansion](temp.variadic#def:pack_expansion "13.7.4Variadic templates[temp.variadic]")[.](#4.sentence-2)
An [*attribute-specifier*](#nt:attribute-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") that contains
an [*attribute-list*](#nt:attribute-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") with no [*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")*s* and no [*alignment-specifier*](#nt:alignment-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") has no effect[.](#4.sentence-3)
The order in which the [*attribute-token*](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")*s* appear in an[*attribute-list*](#nt:attribute-list "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") is not significant[.](#4.sentence-4)
If a[keyword](lex.key "5.12Keywords[lex.key]") or an [alternative token](lex.digraph "5.9Alternative tokens[lex.digraph]") that satisfies the syntactic requirements
of an [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]") ([[lex.name]](lex.name "5.11Identifiers")) is
contained in
an [*attribute-token*](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]"), it is considered an identifier[.](#4.sentence-5)
No[name lookup](basic.lookup "6.5Name lookup[basic.lookup]") is performed on any of the identifiers contained in an[*attribute-token*](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")[.](#4.sentence-6)
The [*attribute-token*](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") determines additional
requirements on the [*attribute-argument-clause*](#nt:attribute-argument-clause "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") (if any)[.](#4.sentence-7)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9488)
An [*annotation*](#nt:annotation "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") followed by an ellipsis
is a pack expansion ([[temp.variadic]](temp.variadic "13.7.4Variadic templates"))[.](#5.sentence-1)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9492)
Each [*attribute-specifier-seq*](#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") is said to [*appertain*](#def:appertain "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") to some entity or
statement, identified by the syntactic context
where it appears ([[stmt]](stmt "8Statements"), [[dcl]](dcl "9Declarations"), [[dcl.decl]](dcl.decl "9.3Declarators"))[.](#6.sentence-1)
If an [*attribute-specifier-seq*](#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") that appertains to some
entity or statement contains an [*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") or [*alignment-specifier*](#nt:alignment-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") that
is not allowed to apply to that
entity or statement, the program is ill-formed[.](#6.sentence-2)
If an [*attribute-specifier-seq*](#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") appertains to a friend declaration ([[class.friend]](class.friend "11.8.4Friends")), that declaration shall be a
definition[.](#6.sentence-3)
[*Note [3](#note-3)*:
An [*attribute-specifier-seq*](#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") cannot appertain to
an explicit instantiation ([[temp.explicit]](temp.explicit "13.9.3Explicit instantiation"))[.](#6.sentence-4)
— *end note*]
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9507)
For an [*attribute-token*](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") (including an [*attribute-scoped-token*](#nt:attribute-scoped-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]"))
not specified in this document, the
behavior is implementation-defined;
any such [*attribute-token*](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") that is not recognized by the implementation
is ignored[.](#7.sentence-1)
[*Note [4](#note-4)*:
A program is ill-formed if it contains an [*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") specified in [[dcl.attr]](dcl.attr "9.13Attributes") that violates
the rules specifying to which entity or statement the attribute can apply or
the syntax rules for the attribute's [*attribute-argument-clause*](#nt:attribute-argument-clause "9.13.1Attribute syntax and semantics[dcl.attr.grammar]"), if any[.](#7.sentence-2)
— *end note*]
[*Note [5](#note-5)*:
The [*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")*s* specified in [[dcl.attr]](dcl.attr "9.13Attributes") have optional semantics:
given a well-formed program,
removing all instances of any one of those [*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")*s* results in a program whose set of possible executions ([[intro.abstract]](intro.abstract "4.1.2Abstract machine"))
for a given input is
a subset of those of the original program for the same input,
absent implementation-defined guarantees
with respect to that [*attribute*](#nt:attribute "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")[.](#7.sentence-3)
— *end note*]
An [*attribute-token*](#nt:attribute-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") is reserved for future standardization if
- [(7.1)](#7.1)
it is not an [*attribute-scoped-token*](#nt:attribute-scoped-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") and
is not specified in this document, or
- [(7.2)](#7.2)
it is an [*attribute-scoped-token*](#nt:attribute-scoped-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") and
its [*attribute-namespace*](#nt:attribute-namespace "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") isstd followed by zero or more digits[.](#7.sentence-4)
Each implementation should choose a distinctive name for the[*attribute-namespace*](#nt:attribute-namespace "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") in an [*attribute-scoped-token*](#nt:attribute-scoped-token "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")[.](#7.sentence-5)
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9542)
Two consecutive left square bracket tokens shall appear only
when introducing an [*attribute-specifier*](#nt:attribute-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") or
within the [*balanced-token-seq*](#nt:balanced-token-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") of
an [*attribute-argument-clause*](#nt:attribute-argument-clause "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")[.](#8.sentence-1)
[*Note [6](#note-6)*:
If two consecutive left square brackets appear
where an [*attribute-specifier*](#nt:attribute-specifier "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") is not allowed, the program is ill-formed even
if the brackets match an alternative grammar production[.](#8.sentence-2)
— *end note*]
[*Example [2](#example-2)*: int p[10];void f() {int x = 42, y[5]; int(p[[x] { return x; }()]); // error: invalid attribute on a nested [*declarator-id*](dcl.decl.general#nt:declarator-id "9.3.1General[dcl.decl.general]") and// not a function-style cast of an element of p. y[[] { return 2; }()] = 2; // error even though attributes are not allowed in this context.int i [[vendor::attr([[]])]]; // well-formed implementation-defined attribute.} — *end example*]