32

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

3
  • Why would you think it would work? Did you see any language specifications?
    – SwiftMango
    Commented Aug 9, 2015 at 23:05
  • And no it doesn't work. It's gcc extension only. Not language specs
    – SwiftMango
    Commented Aug 9, 2015 at 23:05
  • It does not perfectly fit your question as an answer but my comment below may interest you. Commented Aug 10, 2015 at 2:21

3 Answers 3

28

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

1
  • Great, with this gcc allows syntax: X x [[gnu::unused]] = y;
    – Aelian
    Commented Sep 20, 2018 at 18:07
28

There is [[maybe_unused]] attribute in C++17. It's implemented in GCC 7, see C++ Standards Support in GCC .

Example from P0212R1 proposal:

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

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:

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

and in C++14:

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

C++ 17 is the version that introduces the required feature:

Example from the link:

#include <cassert>
 
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
                        [[maybe_unused]] bool thing2)
{
    [[maybe_unused]] bool b = thing1 && thing2;
    assert(b); // in release mode, assert is compiled out, and b is unused
               // no warning because it is declared [[maybe_unused]]
} // parameters thing1 and thing2 are not used, no warning

So the answer is: no, it's not possible, using only C++11 features - the required C++ version to get this in a portable way is C++ 17.


If you are not interested only in portable solutions, there might be a way. C++ standard does not limit this list:

Only the following attributes are defined by the C++ standard. All other attributes are implementation-specific.

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

  • [[gnu::unused]]

Perhaps your version of GCC also supports this specifier. This page contains a bug report referring to generalized attributes support. [[gnu::unused]] is also mentioned.

5
  • Ben Deane came up with an interesting C++11 solution which I added as an answer here, it obviously relies on the optimizer but I have not seen one that does not do the right thing yet. Commented Aug 10, 2015 at 2:20
  • @ShafikYaghmour Interesting idea, although I don't think it is a good solution. It may or may not be optimized out in such case. unused attribute is more explicit and predictable. Also, every known platform has some form of unused, so the best multiplatform solution would be an #ifdef and common macro for all platforms - it's probably the best possible solution (until more attributes will be standardized). Commented Aug 10, 2015 at 2:39
  • I would probably file a bug report if it was not properly optimized away, I am not sure I would use that trick but it is worth documenting it may end up very useful to someone else. Commented Aug 10, 2015 at 2:43
  • Are there any changes for C++17 (C++1z)?
    – Macxx
    Commented Aug 8, 2017 at 12:06
  • The edit queue's full so I can't do anything, but the MSDN link currently redirects to learn.microsoft.com/en-us/previous-versions/… and should be updated when editing is possible again.
    – AJM
    Commented Jun 1, 2022 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.