Under gcc/g++ 4.9 I can write:
int x __attribute__((unused)) = f();
to indicate that x is intentionally unused.
Is it possible to do this with the C++11 [[]]
attribute notation somehow?
I tried:
int x [[unused]] = f();
but it doesn't work.
(Yes, I know it is an implementation-defined attribute.)
__attribute__((unused)) variable attribute Normally, the compiler warns if a variable is declared but is never referenced. This attribute informs the compiler that you expect a variable to be unused and tells it not to issue a warning if it is not used.
Attributes are modern ways in C++ to standardize things if their code runs on different compilers. Attributes are used to provide some extra information that is used to enforce conditions (constraints), optimization and do specific code generation if required.
2 Answers. Show activity on this post. The attribute noreturn (however it is decorated) means that when this function is called, the flow of control will never return from the function. That typically means that it, in turn, calls a function such as exit() or _exit() or something similar that stops the program.
If a compiler doesn't recognize an attribute (a gcc attribute when compiling on MSVC as example) it'll simply be ignored. (probably with a warning) For gcc you can use the gnu prefix and the C++11 attribute syntax: [ [gnu::unused]] instead of __attribute__ ( (unused)) the same should apply for the other gcc attributes too.
This attribute, attached to a function, means that the function is meant to be possibly unused. GCC will not produce a warning for this function. This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced.
The target attribute is not implemented in GCC versions earlier than 4.4 for the i386/x86_64 and 4.6 for the PowerPC backends. It is not currently implemented for other backends. Use this attribute on the H8/300H and H8S to indicate that the specified variable should be placed into the tiny data section.
The thing you are referring to is known as attribute specifiers. It is an attempt to standardize various, platform dependent, specifiers: As you can see in attached doc link, the only specifiers supported in C++11 are: [ [deprecated]] (also supported as: [ [deprecated ("reason")]])
Yes, use [[gnu::unused]]
Like already said unused
isn't part of the standard attributes specified by the standard.
The standard allows implementation defined attributes too like the __attribute__
and __declspec
ones to be used with the new syntax. If a compiler doesn't recognize an attribute (a gcc attribute when compiling on MSVC as example) it'll simply be ignored. (probably with a warning)
For gcc you can use the gnu prefix and the C++11 attribute syntax: [[gnu::unused]]
instead of __attribute__((unused))
the same should apply for the other gcc attributes too.
example without gnu prefix
example with gnu prefix
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With