如何将#pragma clang 属性推送与 C++ 命名空间一起使用?
How does one use #pragma clang attribute push with C++ namespaces?
LLVM 的 clang/clang++ 允许您 specify attributes for entire regions of code。语法如下:
// the following works fine under clang:
#pragma clang attribute push(__attribute__((target("sse4.2"))), apply_to=function)
void f(){}
#pragma clang attribute pop
在此示例中,函数 f
将编译为支持 SSE4.2 指令集。这适用于 attribute push
和 attribute pop
之间的所有函数。
如果我们添加一个命名空间呢?
namespace joe {
#pragma clang attribute push(__attribute__((target("sse4.2"))), apply_to=function)
void g() {}
#pragma clang attribute pop
}
它失败了 'error: expected unqualified-id'
。显然关键字 attribute
不是编译器所期望的。
考虑到命名空间的普遍性,这个问题有点出乎意料。当然,可以避免命名空间来解决这个问题,但它有点不雅。
是否有更好的解决方法?
我已经用所有最新的 LLVM clang++ 编译器(最高 8.0)验证了这个问题。
这似乎在 clang 9 (2019-09) 中有效,但在以下版本之前无效:https://gcc.godbolt.org/z/okfDiS
LLVM 的 clang/clang++ 允许您 specify attributes for entire regions of code。语法如下:
// the following works fine under clang:
#pragma clang attribute push(__attribute__((target("sse4.2"))), apply_to=function)
void f(){}
#pragma clang attribute pop
在此示例中,函数 f
将编译为支持 SSE4.2 指令集。这适用于 attribute push
和 attribute pop
之间的所有函数。
如果我们添加一个命名空间呢?
namespace joe {
#pragma clang attribute push(__attribute__((target("sse4.2"))), apply_to=function)
void g() {}
#pragma clang attribute pop
}
它失败了 'error: expected unqualified-id'
。显然关键字 attribute
不是编译器所期望的。
考虑到命名空间的普遍性,这个问题有点出乎意料。当然,可以避免命名空间来解决这个问题,但它有点不雅。
是否有更好的解决方法?
我已经用所有最新的 LLVM clang++ 编译器(最高 8.0)验证了这个问题。
这似乎在 clang 9 (2019-09) 中有效,但在以下版本之前无效:https://gcc.godbolt.org/z/okfDiS