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
在此示例中,将在支持SSE4.2指令集的情况下编译函数f
。这将适用于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