【发布时间】:2019-07-27 01:44:21
【问题描述】:
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)验证了这个问题。
【问题讨论】:
-
对我来说看起来像一个愚蠢的解析器错误。最简单的事情可能就是修复 C++ 解析器。