1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
Attributes hi(attribute) are compiler directives that are inserted into source
files to inform the compiler of some peculiarity of the code (variable or
function) that follows the specified attribute. Attributes are used to inform
the compiler about situations that are intentional, and thus prevent the
compiler from issuing warnings.
The following attributes are recognized:
itemization(
ithtq(carries_dependency)([[carries_dependency]])
(This attribute is currently not yet covered by the annotations(). At
this point in the annotations() it can safely be ignored.
COMMENT(
tt([[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
END)
)
ithtq(deprecated)([[deprecated]])
(This attribute (and its alternative form
tt([[deprecated("reason")]])) is available since the C++14 standard. It
indicates that the use of the name or entity declared with this attribute is
allowed, but discouraged for some reason. This attribute can be used for
classes, typedef-names, variables, non-static data members, functions,
enumerations, and template specializations. An existing non-deprecated entity
may be redeclared deprecated, but once an entity has been declared deprecated
it cannot be redeclared as `undeprecated'. When encountering the
tt([[deprecated]]) attribute the compiler generates a warning, e.g.,
verb( demo.cc:12:24: warning: 'void deprecatedFunction()' is deprecated
[-Wdeprecated-declarations] deprecatedFunction();
demo.cc:5:21: note: declared here
[[deprecated]] void deprecatedFunction())
When using the alternative form (e.g.,
tt([[deprecated("do not use")]] void fun())) the compiler generates a
warning showing the text between the double quotes, e.g.,
verb( demo.cc:12:24: warning: 'void deprecatedFunction()' is deprecated:
do not use [-Wdeprecated-declarations]
deprecatedFunction();
demo.cc:5:38: note: declared here
[[deprecated("do not use")]] void deprecatedFunction())
)
itt([[fallthrough]])hi(fallthrough)nl()
When statements nested under tt(case) entries in tt(switch) statements
continue into subsequent tt(case) or tt(default) entries the compiler issues a
`falling through' warning. If falling through is intentional the attribute
tt([[fallthrough]]), which then must be followed by a semicolon, should be
used. Here is an annotated example:
verb(void function(int selector)
{
switch (selector)
{
case 1:
case 2: // no falling through, but merged entry points
cout << "cases 1 and 2\n";
[[fallthrough]]; // no warning: intentionally falling through
case 3:
cout << "case 3\n";
case 4: // a warning is issued: falling through not
// announced.
cout << "case 4\n";
[[fallthrough]]; // error: there's nothing beyond
}
})
itt([[maybe_unused]])hi(maybe_unused)nl()
This attribute can be applied to a class, typedef-name, variable,
parameter, non-static data member, a function, an enumeration or an
enumerator. When it is applied to an entity no warning is generated when the
entity is not used. Example:
verb(void fun([[maybe_unused]] size_t argument)
{
// argument isn't used, but no warning
// telling you so is issued
})
itt([[nodiscard]])hi(nodiscard)nl()
The attribute tt([[nodiscard]]) may be specified when declaring a
function, class or enumeration. If a function is declared tt([[nodiscard]]) or
if a function returns an entity previously declared using tt([[nodiscard]])
then the return value of such a function may only be ignored when explicitly
cast to void. Otherwise, when the return value is not used a warning is
issued. Example:
verb(int [[nodiscard]] importantInt();
struct [[nodiscard]] ImportantStruct { ... };
ImportantStruct factory();
int main()
{
importantInt(); // warning issued
factory(); // warning issued
})
ithtq(noreturn)([[noreturn]])
(tt([[noreturn]]) indicates that the function does not
return. tt([[noreturn]]'s) behavior is undefined if the function declared with
this attribute actually returns. The following standard functions have this
attribute: tt(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), Here is an
example of a function declaration and definition using the tt([[noreturn]])
attribute:
verb( [[noreturn]] void doesntReturn();
[[noreturn]] void doesntReturn()
{
exit(0);
})
)
)
|