I was trying to use the GCC attributes with the C++11 syntax. For example something like this:
static void [[used]] foo(void)
{
// ...
}
But I get the following:
warning: ‘used’ attribute ignored [-Wattributes]
static void [[used]] foo(void)
^
Why is the attribute ignored? Is it possible to use GCC attributes as C++ attributes?
[[gnu::used]] static void foo(void) {}
First, the attribute can only appear in specific places, otherwise you 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
is not a standard warning, so it gets hidden in a proprietary namespace gnu::
.
There is no [[used]]
attribute in C++ 11, that's why it's being ignored. (*)
There is gcc-specific __attribute__((used))
, that can be applied to static object or function definitions. It tells compiler to emit definitions, even if that symbol seems to be unused at all - in other words, it makes you sure, that such symbol will be present in result object file.
(*) It needs to be ignored, because standard allows implementations to define additional, implementation-specific attributes. So there is no point in treating unknown attributes as an error (similar case: #pragma
directives).
Some additional info:
Attributes provide the unified standard syntax for implementation-defined language extensions, such as the GNU and IBM language extensions
__attribute__((...))
, Microsoft extension__declspec()
, etc.
And, probably the most important part:
Only the following attributes are defined by the C++ standard. All other attributes are implementation-specific.
[[noreturn]]
[[carries_dependency]]
[[deprecated]]
(C++14)[[deprecated("reason")]]
(C++14)
Source: attribute specifier sequence.
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