Language feature: Attributes

code

Syntax

Attributes are something that provides some warnings or restraints to the compiler, and inform the reader of the specific situation.

The following attributes were defined in C++14 and C++17:

Deprecated

Indicates that the use of the name or entity declared with this attribute is allowed, but discouraged for some reason. The string literals that can be added into the expression can be used to advice on alternatives.

fallthrough

In case we don’t add break between cases, fallthrough will occur, and the compiler will complain about this. In order to assure it that we know what we’re doing (Both the compiler and the reader of this code), we can use the fallthrough attribute.

nodiscard

Nodiscard advices the compiler to throw a warning in case we do not interrogate the return value of a function marked with nodiscard.

maybe_unused

The attribute suppresses the compiler warning for the parameters and locals that are there but they are not unused in the code.

Leave a comment

Your email address will not be published.