Title
Applying [[maybe_unused]] to a label
Status
drwp
Section
9.12.9 [dcl.attr.unused]
Submitter
Barry Revzin

Created on 2023-05-25.00:00:00 last changed 7 months ago

Messages

Date: 2023-11-07.21:09:24

CWG 2023-07-14

CWG has reviewed and approved the proposed resolution. However, this is a new (albeit small) feature, thus forwarding to EWG via paper issue 1585 for approval.

EWG 2023-11-07

Accept the proposed resolution, forward to CWG for inclusion in C++26.

Date: 2023-07-14.21:51:01

Proposed resolution (approved by CWG 2023-07-14)

Change in 9.12.9 [dcl.attr.unused] as follows:

The attribute-token maybe_unused indicates that a name, label, or entity is possibly intentionally unused. No attribute-argument-clause shall be present.

The attribute may be applied to the declaration of a class, a typedef-name, a variable (including a structured binding declaration), a non-static data member, a function, an enumeration, or an enumerator, or to an identifier label (8.2 [stmt.label]).

A name or entity declared without the maybe_unused attribute can later be redeclared with the attribute and vice versa. An entity is considered marked after the first declaration that marks it.

Recommended practice: For an entity marked maybe_unused, implementations should not emit a warning that the entity or its structured bindings (if any) are used or unused. For a structured binding declaration not marked maybe_unused, implementations should not emit such a warning unless all of its structured bindings are unused. For a label to which maybe_unused is applied, implementations should not emit a warning that the label is used or unused.

[Example 1:
  [[maybe_unused]] void f([[maybe_unused]] bool thing1,
                          [[maybe_unused]] bool thing2) {
    [[maybe_unused]] bool b = thing1 && thing2;
    assert(b);
#ifdef NDEBUG
    goto x;
#endif
    [[maybe_unused]] x:
  }
Implementations should not warn that b or x is unused, whether or not NDEBUG is defined. — end example]
Date: 2023-11-15.00:00:00

[Accepted as a DR at the November, 2023 meeting.]

Subclause 9.12.9 [dcl.attr.unused] paragraph 2 specifies:

The attribute may be applied to the declaration of a class, a typedef-name, a variable (including a structured binding declaration), a non-static data member, a function, an enumeration, or an enumerator.

Absent from that list are labels, but both gcc and clang accept [[maybe_unused]] on a label, and behave accordingly.

History
Date User Action Args
2024-04-05 21:43:46adminsetstatus: dr -> drwp
2023-12-19 10:15:28adminsetstatus: ready -> dr
2023-11-08 06:04:36adminsetstatus: review -> ready
2023-07-14 21:51:01adminsetmessages: + msg7364
2023-06-14 16:20:56adminsetmessages: + msg7320
2023-06-14 16:20:56adminsetstatus: open -> review
2023-05-25 00:00:00admincreate