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

后端 未结 3 1138
眼角桃花
眼角桃花 2021-01-01 12:59

Under gcc/g++ 4.9 I can write:

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

to indicate that x is intentionally unused.

Is it possible to d

3条回答
  •  Happy的楠姐
    2021-01-01 13:23

    The thing you are referring to is known as attribute specifiers. It is an attempt to standardize various, platform dependent, specifiers:

    • __attribute__ in case of GCC / ICC (Linux)
    • __declspec on MSVC / ICC (Windows)

    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")]])

    So the answer is: no, it's not possible, using only C++11 features.


    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.

提交回复
热议问题