是否有 gcc 的 __attribute__ (纯)的可移植等效项?
我正在编写一些代码,其中有一堆被频繁调用的简单纯函数。如果这些函数得到优化以减少调用频率,那么这是完全安全的。
目前我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 C++11 开始,您可以将标准化属性语法与 GCC 特定属性结合使用:
从 C++17 开始,这在任何编译器上都保证没问题,因为如果它们不能识别
[[gnu::pure]]
,它们必须忽略它而不会出错。Since C++11, you can use the standardized attribute syntax with GCC specific attributes:
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.不,没有。
No, there is not.
当你需要的时候使用
__pure
Use
__pure
when you need it我认为可移植的方法是内联函数并希望编译器能够弄清楚其余的事情。
I think the portable way is to inline the functions and hope the compiler will figure out the rest.