将 gcc 属性与 C++11 属性语法一起使用
Using gcc attributes with C++11 attributes syntax
我正在尝试使用 GCC attributes with the C++11 syntax。例如这样的事情:
static void [[used]] foo(void)
{
// ...
}
但我得到以下信息:
warning: ‘used’ attribute ignored [-Wattributes]
static void [[used]] foo(void)
^
为什么属性被忽略了?是否可以将 GCC 属性用作 C++ 属性?
gcc 属性与 C++11 中引入的属性不同。
used
是特定于 gcc 的属性,应使用 gcc 属性语法 __attribute__((used))
引入。标准 C++ 中没有 [[used]]
属性,因此 gcc 将忽略它。
C++ 11 中没有 [[used]]
属性,这就是它被忽略的原因。 (*)
有特定于 gcc 的 __attribute__((used))
,可应用于静态对象或函数定义。它告诉编译器发出定义,即使该符号似乎根本未使用 - 换句话说,它使您确定该符号将出现在结果目标文件中。
(*) 它需要被忽略,因为标准允许实现定义额外的、特定于实现的属性。因此,将未知属性视为错误是没有意义的(类似情况:#pragma
指令)。
一些附加信息:
Attributes provide the unified standard syntax for implementation-defined language extensions, such as the GNU and IBM language extensions __attribute__((...))
, Microsoft extension __declspec()
, etc.
而且,可能是最重要的部分:
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)
[[gnu::used]] static void foo(void) {}
首先,属性只能出现在特定的地方,否则你会得到:
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
其次,used
不是标准警告,因此它隐藏在专有名称空间中 gnu::
。
我正在尝试使用 GCC attributes with the C++11 syntax。例如这样的事情:
static void [[used]] foo(void)
{
// ...
}
但我得到以下信息:
warning: ‘used’ attribute ignored [-Wattributes]
static void [[used]] foo(void)
^
为什么属性被忽略了?是否可以将 GCC 属性用作 C++ 属性?
gcc 属性与 C++11 中引入的属性不同。
used
是特定于 gcc 的属性,应使用 gcc 属性语法 __attribute__((used))
引入。标准 C++ 中没有 [[used]]
属性,因此 gcc 将忽略它。
C++ 11 中没有 [[used]]
属性,这就是它被忽略的原因。 (*)
有特定于 gcc 的 __attribute__((used))
,可应用于静态对象或函数定义。它告诉编译器发出定义,即使该符号似乎根本未使用 - 换句话说,它使您确定该符号将出现在结果目标文件中。
(*) 它需要被忽略,因为标准允许实现定义额外的、特定于实现的属性。因此,将未知属性视为错误是没有意义的(类似情况:#pragma
指令)。
一些附加信息:
Attributes provide the unified standard syntax for implementation-defined language extensions, such as the GNU and IBM language extensions
__attribute__((...))
, Microsoft extension__declspec()
, etc.
而且,可能是最重要的部分:
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)
[[gnu::used]] static void foo(void) {}
首先,属性只能出现在特定的地方,否则你会得到:
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
其次,used
不是标准警告,因此它隐藏在专有名称空间中 gnu::
。