Refining [[pure]] in C ++

Reading the last sentence for the standard C ++ attribute [[pure]] I still have a number of questions:

  • Can the [[pure]] function read constant global variables?
  • Can the [[pure]] function read static constant variables?
  • Can the [[pure]] function write, but not read, pointers or reference variables (and possibly return void)?
+4
source share
1 answer

[[pure]]. If I remember correctly, I must be 100% sure that a particular input should always return a specific output. Given that const globals and static const globals should never change states, this should be fine. Finally, reading a pointer that can change states will not be a pure function. If you write to pointers available elsewhere, then it has a side effect and is not [[pure]].

You must ask yourself if the result of the function is dependent on the state of extraneous variables, and if you answer yes, it is not [[pure]]. You should also ask yourself if the input can influence anything other than the return value. If this is true, then it is not [[pure]]

+4
source

All Articles