开发者

Is there a portable equivalent of gcc's __attribute__ (pure)?

开发者 https://www.devze.com 2023-01-18 17:01 出处:网络
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.

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.

Currentl开发者_如何学编程y 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?


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.


No, there is not.


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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消