【问题标题】:How does one use #pragma clang attribute push with C++ namespaces?如何将#pragma clang 属性推送与 C++ 命名空间一起使用?
【发布时间】: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 pushattribute 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)验证了这个问题。

I have created a gist to illustrate the issue.

【问题讨论】:

  • 对我来说看起来像一个愚蠢的解析器错误。最简单的事情可能就是修复 C++ 解析器。

标签: clang clang++


【解决方案1】:

这似乎在 clang 9 (2019-09) 中有效,但在此之前无效:https://gcc.godbolt.org/z/okfDiS

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 2016-09-14
  • 2021-08-26
  • 1970-01-01
  • 2019-01-17
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多