And*_*chi 7 c++ attributes gcc c++11
static void [[used]] foo(void)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
但我得到以下内容:
warning: ‘used’ attribute ignored [-Wattributes]
static void [[used]] foo(void)
^
Run Code Online (Sandbox Code Playgroud)
为什么忽略该属性?是否可以将GCC属性用作C++属性?
Mar*_*sse 11
[[gnu::used]] static void foo(void) {}
Run Code Online (Sandbox Code Playgroud)
首先,属性只能出现在特定的地方,否则你会得到:
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
Run Code Online (Sandbox Code Playgroud)
其次,used
它不是标准警告,因此它隐藏在专有命名空间中gnu::
.
[[used]]
C ++ 11中没有属性,这就是为什么它被忽略的原因。(*)
有gcc-specific __attribute__((used))
,可以将其应用于静态对象或函数定义。它告诉编译器发出定义,即使该符号似乎根本没有用过-换句话说,您可以确保该符号将出现在结果对象文件中。
(*)需要忽略,因为标准允许实现定义其他特定于实现的属性。因此,将未知属性视为错误是没有意义的(类似情况:#pragma
指令)。
一些其他信息:
属性为实现定义的语言扩展(例如GNU和IBM语言扩展
__attribute__((...))
,Microsoft扩展__declspec()
等)提供统一的标准语法。
并且,可能是最重要的部分:
C ++标准仅定义以下属性。所有其他属性都是特定于实现的。
[[noreturn]]
[[carries_dependency]]
[[deprecated]]
(C ++ 14)[[deprecated("reason")]]
(C ++ 14)
来源:属性说明符序列。