Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Preprocessor.h File Reference

Defines a set of useful preprocessor macros. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PURITY_WEAK   [[gnu::pure]]
 
#define PURITY_STRONG   [[gnu::const]]
 

Detailed Description

Defines a set of useful preprocessor macros.

Note
When applying the contained function attributes, remember that exception branch correctness must be considered during e.g. common subexpression elimination. It is helpful in many cases to additionally annotate low-level often-used functions with the noexcept specifier (if possible) to enable more optimizations.
MSVC does not implement any of these attributes, so we must replace them with empty defines if compiling with it.

Macro Definition Documentation

#define PURITY_STRONG   [[gnu::const]]

Applies a strong purity function attribute

The strong purity function attribute implies all weak purity implications AND

  • the return value depends only on function arguments
  • its arguments are values, not pointers or references
  • does not call non-pure or weak purity attributed functions
Warning
This attribute is unchecked! Applying this attribute to functions that do not adhere to the contract leads to undefined behavior.
Be especially careful with classes. Simple getter functions that return a member by value cannot be strongly pure as they refer to state that is not encompassed by the function arguments. They may be weakly pure, however.
Be REALLY careful with this attribute on template classes' members. Make no unfounded assumptions about the template parameters behavior. If needed, constrain the template parameter with SFINAE.
Any const class member function without arguments CANNOT be of strong purity, since this is a pointer. Any access to class members within the function violates the contract.
Note
Do not expect compilers to aggressively take use of purity attributes without also providing exception guarantees. See https://kristerw.blogspot.com/2016/12/gcc-attributepure-and-c-exceptions.html
#define PURITY_WEAK   [[gnu::pure]]

Applies a weak purity function attribute

The weak purity function attribute implies

  • return value depends only on arguments and/or global variables (class member variables are implicitly an argument, as this* is captured)
  • does not depend on volatile values
  • does not cause side effects (and does not call any impure functions)
  • is safe to call fewer times than the program says
Warning
This attribute is unchecked! Applying this attribute to functions that do not adhere to the contract leads to undefined behavior.
Note
Do not expect compilers to aggressively take use of purity attributes without also providing exception guarantees. See https://kristerw.blogspot.com/2016/12/gcc-attributepure-and-c-exceptions.html