Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22144

GNU const/pure attributes vs constexpr

$
0
0

GNU C and C++ offer the const and pure function attributes. From the gnu online docs (emphasis mine):

In GNU C and C++, you can use function attributes to specify certain function properties that may help the compiler optimize calls or check code more carefully for correctness. For example, you can use attributes to specify that a function never returns (noreturn), returns a value depending only on the values of its arguments (const), or has printf-style arguments (format).

Where the const attribute seems to be a superset to pure, also taken from the gnu docs:

The const attribute imposes greater restrictions on a function’s definition than the similar pure attribute. Declaring the same function with both the const and the pure attribute is diagnosed.

With C++ 11, the constexpr specifier was added.

When applied to functions, is there a difference between the const attribute and the constexpr specifier? Does GCC apply different optimizations?

A similar sounding question is Difference between `constexpr` and `const` . But I think this is not a duplicate. My question is specifically about the function attribute const, which seems to have overlapping functionality with constexpr.


Viewing all articles
Browse latest Browse all 22144

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>