Martin Ueding Martin Ueding - 12 days ago 6
C++ Question

Implicit “using namespace std” without writing it in the source code

I have a C++ code that uses

endl
,
vector
,
complex
, and
cout
occasionally without any namespace qualification. When compiling with GCC 4.9, I get errors like this one:

bfm.h:454:4: error: 'complex' does not name a type
complex<double> inner(Fermion_t x,Fermion_t y);
^


In the very same file, there are lines with an
std::
qualifier:

std::complex<Float> dot(Fermion_t x_t, Fermion_t y_t,int sx,int sy);


Throughout the codebase, I saw the following template specializations used


  • std::complex<double>

  • std::complex<cFloat>

  • complex<Float>

  • complex<double>

  • complex<T>

  • complex<S>
    .



Neither for the regular express
struct\s+complex
nor
class\s+complex
I managed to find something in the codebase or the codebase of the base library. Therefore I assume that it indeed is the standard library complex type.

There are a couple of
using namespace std
scattered around in various header files, but not all of them.

I tried compiling this codebase with GCC 4.9 and Clang 3.7. Both give similar errors about the missing type. Is there some possibility that this has worked with an older version of GCC? I have tried to insert
std::
at all the needed points but I have the impression that I do something wrong if I cannot just compile the source checkout that is meant for this type of computer.

Answer

you can use selective using... declarations or type aliasing to bring in only the std:: members you need. Like:

using std::complex; 
using std::cout;
using std::endl;

cout << "Hello world" << endl; // works
complex<float> x; // works

fstream y; // compile error, no namespace qualification, no using declaration
std::fstream z; // OK