是否有 gcc 的 __attribute__ (纯)的可移植等效项?

发布于 2024-09-25 15:56:33 字数 390 浏览 6 评论 0原文

我正在编写一些代码,其中有一堆被频繁调用的简单纯函数。如果这些函数得到优化以减少调用频率,那么这是完全安全的。

目前我正在使用 gcc 作为编译器,我想知道是否有一种可移植的方法:

int foo(int) __attribute__ ((pure))

可以在此处找到有关 pure 关键字的信息: http://www.ohse.de/uwe/articles/gcc -attributes.html#func-pure

如果 pure 关键字不可用,我将如何实现这样的东西?

I'm writing some code where there are a bunch of simple pure functions that get called a lot. It's perfectly safe if these functions get get optimized to be called less often.

Currently I am using gcc as my compiler and I'm wondering if there is a portable way of doing:

int foo(int) __attribute__ ((pure))

Info about the pure keyword can be found here:
http://www.ohse.de/uwe/articles/gcc-attributes.html#func-pure

How would I go about implementing something like this if the pure keyword is not available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海少女心 2024-10-02 15:56:33

从 C++11 开始,您可以将标准化属性语法与 GCC 特定属性结合使用:

[[gnu::pure]]
int foo(int)

从 C++17 开始,这在任何编译器上都保证没问题,因为如果它们不能识别 [[gnu::pure]],它们必须忽略它而不会出错。

Since C++11, you can use the standardized attribute syntax with GCC specific attributes:

[[gnu::pure]]
int foo(int)

Since C++17, this is guaranteed to be fine on any compiler, since if they don't recognize [[gnu::pure]], they must ignore it without error.

北陌 2024-10-02 15:56:33

不,没有。

No, there is not.

留蓝 2024-10-02 15:56:33
#ifdef __GNUC__
#define __pure __attribute__((pure))
#else
#define __pure
#endif

当你需要的时候使用__pure

#ifdef __GNUC__
#define __pure __attribute__((pure))
#else
#define __pure
#endif

Use __pure when you need it

梨涡少年 2024-10-02 15:56:33

我认为可移植的方法是内联函数并希望编译器能够弄清楚其余的事情。

I think the portable way is to inline the functions and hope the compiler will figure out the rest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文