title: Why is my non-recursive sqrt function recursive?
body: <p>I have the following C++ test program called test.cpp:</p>
  
  <pre><code>#include &lt;cmath&gt;
  #include &lt;iostream&gt;
  
  double sqrt(double d) { return std::sqrt(d); }
  
  int main()
  {
      std::cout &lt;&lt; "sqrt(4): " &lt;&lt; sqrt(4) &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>This is some pretty contrived code, and as you might have guessed I'm just trying to do an exercise out of Stroustrup. He declared double sqrt(double), and wants the reader to define it.</p>
  
  <p>I compiled the above code using g++ 4.8 (from the MINGW release of Qt 5.1):</p>
  
  <pre><code>C:\Windows\Temp&gt;g++ -o test.exe -g test.cpp
  </code></pre>
  
  <p>When I ran the resulting executable, Windows 7 said "test.exe has stopped working".</p>
  
  <p>To see what went wrong, I ran test.exe in the GNU debugger. Debugger commands and output:</p>
  
  <pre><code>C:\Windows\Temp&gt;gdb -q test.exe
  Reading symbols from C:\Windows\Temp\test.exe...done.
  (gdb) b main
  Breakpoint 1 at 0x401625: file test.cpp, line 8.
  (gdb) run
  Starting program: C:\Windows\Temp\test.exe
  [New Thread 12080.0x2ba0]
  
  Breakpoint 1, main () at test.cpp:8
  8           std::cout &lt;&lt; "sqrt(4): " &lt;&lt; sqrt(4) &lt;&lt; std::endl;
  (gdb) s
  sqrt (d=4) at test.cpp:4
  4       double sqrt(double d) { return std::sqrt(d); }
  (gdb) s
  sqrt (d=4) at test.cpp:4
  4       double sqrt(double d) { return std::sqrt(d); }
  (gdb) s
  sqrt (d=4) at test.cpp:4
  4       double sqrt(double d) { return std::sqrt(d); }
  (gdb) s
  sqrt (d=4) at test.cpp:4
  4       double sqrt(double d) { return std::sqrt(d); }
  (gdb) q
  A debugging session is active.
  
          Inferior 1 [process 12080] will be killed.
  
  Quit anyway? (y or n) y
  
  C:\Windows\Temp&gt;
  </code></pre>
  
  <p>From the behavior and warning, I infer that std::sqrt must be calling sqrt from the global namespace -- which causes my function to be repeatedly invoked.</p>
  
  <p>It would be easy enough to work around the unwanted recursion by changing the name of my sqrt function, or by putting it inside a namespace. But I would like to understand why std::sqrt is implemented in such a way that ::sqrt is called. I thought the whole point of the std namespace was to prevent name clashes with unqualified names in user code.</p>
  
  <p>I took a peek at the source code for the <a href="http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.4/cmath-source.html">GNU implementation of &lt;cmath&gt;</a>. However, I lost the trail after following a few #includes in the chain. Maybe you can make more sense of it:</p>
  
  <pre><code>00052 #include &lt;math.h&gt;
  00053 
  00054 // Get rid of those macros defined in &lt;math.h&gt; in lieu of real functions.
  ....
  00076 #undef sqrt
  ....
  00081 namespace std
  00082 {
  ....
  00393   using ::sqrt;
  00394 
  00395   inline float
  00396   sqrt(float __x)
  00397   { return __builtin_sqrtf(__x); }
  00398 
  00399   inline long double
  00400   sqrt(long double __x)
  00401   { return __builtin_sqrtl(__x); }
  00402 
  00403   template&lt;typename _Tp&gt;
  00404     inline typename __enable_if&lt;double, __is_integer&lt;_Tp&gt;::_M_type&gt;::_M_type
  00405     sqrt(_Tp __x)
  00406     { return __builtin_sqrt(__x); }
  ....
  00437 }
  </code></pre>
  
  <p>Incidentally, this is not just a GNU puzzle. Compiling with the Visual C++ compiler instead of g++ yields the following warning:</p>
  
  <pre><code>C:\Windows\Temp&gt;cl /nologo /EHsc test.cpp
  test.cpp
  c:\windows\temp\test.cpp(4) : warning C4717: 'sqrt' : recursive on all control
  paths, function will cause runtime stack overflow
  </code></pre>
  
  <p>I guess that makes this a fair question to ask on StackOverflow. :)</p>
  
  <p>Running the resulting executable leads to the expected outcome: "test.exe has stopped working".</p>