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?
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.)