22

C++17 introduces the attribute [[maybe_unused]].
I assume this a standardized version of GCC and Clang's: __attribute__((unused)).

For unused functions that I don't want to see a warning from,
should I be specifying the attribute on

function declarations?

void maybe_used_function() [[maybe_unused]];

or function definitions?

void maybe_used_function() [[maybe_unused]] {
  /* impl */
}

Either one? Both?
Will the effect be the same on both the standardized and compiler specific attributes?
I can't find any clear documentation on placement behaviour, and what the common practice is.


When I place the attribute before the function body in a definition, GCC and clang give an error:

void function();
int main(){}
void function() __attribute__((unused)) {}  

warning: GCC does not allow 'unused' attribute in this position on a function definition [-Wgcc-compat] void function() __attribute__((unused)) {


However, the attribute can be placed in two other places without error:

__attribute__((unused)) void __attribute__((unused)) function() {}

Maybe one of these ways is how I'm expected to use the attribute on function definitions?

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • Did you *try* and see what the compiler actually says? AFAIK, the attributes go on the declarations *only*, but I didn't double check the standard text. – Jesper Juhl Jul 21 '16 at 21:03
  • @JesperJuhl : The compiler is wrong in this case and this is far from unusual, especially when discussing _upcoming_ language features that may or may not be implemented correctly (or at all). – ildjarn Jul 22 '16 at 01:09
  • @ildjarn the GCC error came from `__attribute__((unused))` not `[[maybe_unused]]`. Clang seems to do the same. – Trevor Hickey Jul 22 '16 at 02:25
  • I wonder why it is important to mark a function [[maybe_unused]]? Shouldn't it be used on variables? – Adam Hunyadi Jun 28 '17 at 08:23
  • if you have an inaccessible function from outside a translation unit, you can get a compiler warning for it not being used. Attributing the function prevents the warning. – Trevor Hickey Jun 28 '17 at 17:12

2 Answers2

31

Neither. In

[[attr1]] void [[attr2]] f [[attr3]] () [[attr4]] {}
  • attr1 and attr3 appertain (or apply) to f itself.
  • attr2 appertains to the preceding type, void.
  • attr4 appertains to f's type ("function of () returning void), not f.

You want maybe_unused to appertain to f, so you can put it in position 1 or 3, but not 2 or 4.

@ildjarn's answer covers the rest.

For GCC's __attribute__, you'll have to check its documentation.

Community
  • 1
  • 1
T.C.
  • 133,968
  • 17
  • 288
  • 421
14

From N4606, [dcl.attr.unused]¶4:

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.

Since a function definition is a declaration ([dcl.dcl]¶1), this means you can put it in either place and it will behave the same.

(It makes sense to be allowed in both places since the attribute only actually affects the definition, but because the attribute can serve as self-documentation it is also permitted on the declaration.)

ildjarn
  • 62,044
  • 9
  • 127
  • 211