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

Viewed 1549

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?

4 Answers

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.

Related