title: Pure/const functions in C++
body: <p>I'm thinking of using pure/const functions more heavily in my C++ code. (<a href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html" rel="nofollow noreferrer">pure/const attribute in GCC</a>)</p>
  
  <p>However, I am curious how strict I should be about it and what could possibly break.</p>
  
  <p>The most obvious case are debug outputs (in whatever form, could be on cout, in some file or in some custom debug class). I probably will have a lot of functions, which don't have any side effects despite this sort of debug output. No matter if the debug output is made or not, this will absolutely have no effect on the rest of my application.</p>
  
  <p>Or another case I'm thinking of is the use of some SmartPointer class which may do some extra stuff in global memory when being in debug mode. If I use such an object in a pure/const function, it does have some slight side effects (in the sense that some memory probably will be different) which should not have any real side effects though (in the sense that the behaviour is in any way different).</p>
  
  <p>Similar also for mutexes and other stuff. I can think of many complex cases where it has some side effects (in the sense of that some memory will be different, maybe even some threads are created, some filesystem manipulation is made, etc) but has no computational difference (all those side effects could very well be left out and I would even prefer that).</p>
  
  <p>So, to summarize, I want to mark functions as pure/const which are not pure/const in a strict sense. An easy example:</p>
  
  <pre><code>int foo(int) __attribute__((const));
  
  int bar(int x) {
     int sum = 0;
     for(int i = 0; i &lt; 100; ++i)
         sum += foo(x);
     return sum;
  }
  
  int foo_callcounter = 0;
  
  int main() {
     cout &lt;&lt; "bar 42 = " &lt;&lt; bar(42) &lt;&lt; endl;
     cout &lt;&lt; "foo callcounter = " &lt;&lt; foo_callcounter &lt;&lt; endl;
  }
  
  int foo(int x) {
     cout &lt;&lt; "DEBUG: foo(" &lt;&lt; x &lt;&lt; ")" &lt;&lt; endl;
     foo_callcounter++;
     return x; // or whatever
  }
  </code></pre>
  
  <p>Note that the function <em>foo</em> is not const in a strict sense. Though, it doesn't matter what <em>foo_callcounter</em> is in the end. It also doesn't matter if the debug statement is not made (in case the function is not called).</p>
  
  <p>I would expect the output:</p>
  
  <pre><code>DEBUG: foo(42)
  bar 42 = 4200
  foo callcounter = 1
  </code></pre>
  
  <p>And without optimisation:</p>
  
  <pre><code>DEBUG: foo(42) (100 times)
  bar 42 = 4200
  foo callcounter = 100
  </code></pre>
  
  <p>Both cases are totally fine because what only matters for my usecase is the return value of bar(42).</p>
  
  <p>How does it work out in practice? <strong>If I mark such functions as pure/const, could it break anything</strong> (considering that the code is all correct)?</p>
  
  <hr>
  
  <p>Note that I know that some compilers might not support this attribute at all. (BTW., I am collecting them <a href="https://stackoverflow.com/questions/2798188/pure-const-function-attributes-in-different-compilers">here</a>.) I also know how to make use of thes attributes in a way that the code stays portable (via #defines). Also, all compilers which are interesting to me support it in some way; so I don't care about if my code runs slower with compilers which do not.</p>
  
  <p>I also know that the optimised code probably will look different depending on the compiler and even the compiler version.</p>
  
  <hr>
  
  <p>Very relevant is also <a href="http://lwn.net/Articles/285332/" rel="nofollow noreferrer">this LWN article "Implications of pure and constant functions"</a>, especially the "Cheats" chapter. <sub>(Thanks ArtemGr for the hint.)</sub></p>