2

Recently C23 added two new attributes: unsequenced and reproducible, which are now supported by GCC alongside the existing pure and const. I am slightly confused by the exact differences between all of them, and all of the sources I could find only compared one or two of them, not all four at once.

Is the following understanding I have of them correct?

Attribute Side Effects Output Read Globals [0] Write Globals [0] Read Pointer Args Write Pointer Args Examples Optimisations
Pure no return yes [1] no yes no strlen, memcpy Elide calls where globals and args have not changed
Const no return const [2] no const [2] no abs All calls with the same arguments have the same value; evaluate at compile-time
Unsequenced yes return const no yes no strlen Reorder calls
Reproducible yes return or args const no yes yes memcpy Elide repeated calls with the same arguments
None yes return or args yes yes yes yes printf None

Sources


Footnotes

[0]: By "globals" I include static and thread_local variables

[1]: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute

functions declared with the pure attribute can safely read any non-volatile objects

[2]: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute

This seems to be allowed, but recommended against:

functions declared with the [const] attribute can safely read objects that do not change their return value, such as non-volatile constants

Note that a function that has pointer arguments and examines the data pointed to must not be declared const if the pointed-to data might change between successive invocations of the function

In general, since a function cannot distinguish data that might change from data that cannot, const functions should never take pointer or, in C++, refrence arguments

3
  • Function attributes are unfortunately severely underspecified to the point where there's no making sense of them. We'll have to wait for Defect Reports and Technical Corrigendum before considering using these experimental features. Commented Jan 7 at 8:00
  • I thought these were released as part of C23; how com they are still experimental features? Commented Jan 12 at 3:05
  • 1
    Because nobody sat down for 5 minutes and considered how they should work in harmony with the existing language. See for example stackoverflow.com/questions/78666782/…. Scratch a bit on the surface of this feature and it comes tumbling down like a house of cards. Significant parts of C23 is a plain fiasco. Commented Jan 13 at 7:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.