Using gcc attributes with c ++ 11 attribute syntax

I tried to use GCC attributes with C ++ 11 syntax . For example, something like this:

static void [[used]] foo(void)
{
    // ...
}

      

But I am getting the following:

warning: ‘used’ attribute ignored [-Wattributes]
static void [[used]] foo(void)
            ^

      

Why is the attribute being ignored? Can GCC attributes be used as C ++ attributes?

+3


source to share


3 answers


[[gnu::used]] static void foo(void) {}

      

First, the attribute can only appear in certain places, otherwise you will get:



x.cc:1:13: warning: attribute ignored [-Wattributes]
 static void [[gnu::used]] foo(void) {}
             ^
x.cc:1:13: note: an attribute that appertains to a type-specifier is ignored

      

Second, used

it is not a standard warning, so it hides itself in the proprietary namespace gnu::

.

+8


source


There is no attribute in C ++ 11 [[used]]

, so it is ignored. (*)

There is gcc-specific __attribute__((used))

which can be applied to static objects or function definitions. It tells the compiler that it emits definitions even if that symbol is not used at all, in other words, it guarantees that such a symbol will be present in the result object file.




(*) It should be ignored because the standard allows implementations to define additional implementation-specific attributes. So there is no point in treating unknown attributes as an error (similar cases: directives #pragma

).




Additional Information:

Attributes provide a unified standard syntax for implementation-defined language extensions, such as the GNU and IBM language __attribute__((...))

extensions, the Microsoft extension, __declspec()

and so on.

And perhaps the most important part:

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

  • [[noreturn]]

  • [[carries_dependency]]

  • [[deprecated]]

    (C ++ 14)
  • [[deprecated("reason")]]

    (C ++ 14)

Source: sequence of attribute specifiers .

+4


source


gcc attributes are not the same as attributes introduced in C ++ 11.

used

It is an attribute gcc-specificity and to be introduced using the syntax gcc attribute __attribute__((used))

. There is no attribute in standard C ++ [[used]]

, so gcc simply ignores it.

+1


source







All Articles