title: Why use an extern "C" around a C++ namespace
body: <p>A few days back, I came across this piece of C++ code, though I can't paste the code itself, I could recreate the problem with some sample code.
  First, the file, namespace.h:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  namespace useless{
    class X {
       int m_myint;
       static X *m_foobar;
       X* getPrivVal(void);
    public:
       int getMember(void);
       X* getStaticVal(void);
    };
  }
  </code></pre>
  
  <p>Next, <code>namespace.cpp</code>:</p>
  
  <pre><code>#include "namespace.h"
  
  extern "C"{
     namespace useless{
       X* X::m_foobar = NULL;
       X* X::getPrivVal(void){
           if(m_foobar == NULL)
               m_foobar = new X;
           return(m_foobar);
       }
     }
  }
  
  namespace useless {
     int X::getMember(void){
         if(m_myint == 0)
             m_myint = 1;
        return(m_myint);
     }
     X* X::getStaticVal(void){
          return(getPrivVal());
     }
  }
  
  using namespace useless;
  
  int main(void){
      X y;
      cout &lt;&lt; "The int value is " &lt;&lt; y.getMember() &lt;&lt; endl;
      cout &lt;&lt; "The value of the static member is " &lt;&lt; y.getStaticVal() &lt;&lt; endl;
      return(0);
  }
  </code></pre>
  
  <p>This code compiled and linked fine when I used g++ 3.4.3, but gives the following error when I use g++ 4.x, I have tried with GCC 4.6.1 as well GCC 4.2.1. Have tried it on Linux as well as Mac, same results.</p>
  
  <p>This is the error(Suse):</p>
  
  <pre><code>g++ -o namespace namespace.cpp
  namespace.h:8:15: error: previous declaration of useless::X* 
      useless::X::m_foobar with C++ linkage
  namespace.cpp:5:11: error: conflicts with new declaration with C linkage
  </code></pre>
  
  <p>Can someone please shed some light on what the legacy C++ code was looking to do, could this have been a hack or workaround to get around an old problem that I am not aware of any longer. Ohh by the way, the method inside the <code>extern "C"</code> is called by C++ code and not any C code as I initially suspected. 
  FYI, this is legacy code now, may have been written in 2001/2002 period.</p>
  
  <hr>
  
  <p>Thanks guys. I have gone ahead and got rid of the extern "C" with no major impact.
  @Bjorn Pollex : The guy who wrote that code is long gone.</p>