C ++ 11-style [[unused]] attribute in gcc?

In gcc / g ++ 4.9, I can write:

int x __attribute__((unused)) = f(); 

to indicate that x is not intentionally used.

Is it possible to do this by somehow specifying the C ++ 11 [[]] attribute?

I tried:

 int x [[unused]] = f(); 

but that will not work.

(Yes, I know this is an attribute defined by the implementation.)

+5
source share
3 answers

Yes, use [[gnu::unused]]

As already mentioned, unused not part of the standard attributes specified by the standard.

The standard also allows you to implement certain implementation attributes, similar to the __attribute__ and __declspec tags that will be used with the new syntax. If the compiler does not recognize the attribute (the gcc attribute when compiling into MSVC as an example), it will simply be ignored. (possibly with a warning)

For gcc, you can use the gnu prefix and C ++ 11 attribute syntax: [[gnu::unused]] instead of __attribute__((unused)) the same should apply to other gcc attributes.

example without gnu prefix

gnu prefix example

+6
source

What you're talking about is called attribute specifiers . This is an attempt to standardize various platform-specific qualifiers:

As you can see in the attached doc link, the only qualifiers supported in C ++ 11 are:

  • [[noreturn]]
  • [[carries_dependency]]

and in C ++ 14:

  • [[deprecated]] (also supported as: [[deprecated("reason")]] )

So the answer is no, this is not possible using only C ++ 11 functions .


If you are not only interested in portable solutions, this may be so. The C ++ standard does not limit this list:

The C ++ standard defines only the following attributes. All other attributes are implementation specific.

Different compilers may support some non-standard specifiers. For example, you can read this page to find out that Clang supports:

  • [[gnu::unused]]

Your version of GCC may also support this specifier. This page contains an error report that references generic attribute support. [[gnu::unused]] .

+8
source

In C ++ 17, there is an attribute [[maybe_unused]] . It is implemented in GCC 7, see Support for C ++ Standards in GCC .

Example from Proposal P0212R1 :

 [[maybe_unused]] void f([[maybe_unused]] bool thing1, [[maybe_unused]] bool thing2) { [[maybe_unused]] bool b = thing1 && thing2; assert(b); } 
+2
source

All Articles