隐式“使用命名空间std”而无需在源代码中编写它

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

本文关键字:源代码 命名空间 std 隐式      更新时间:2023-10-16

我有一个C 代码,该代码使用endlvectorcomplexcout 有时 没有任何名称空间资格。使用GCC 4.9编译时,我会得到这样的错误:

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

在同一文件中,有std::预选赛的行:

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

在整个代码库中,我看到了以下模板专业

  • std::complex<double>
  • std::complex<cFloat>
  • complex<Float>
  • complex<double>
  • complex<T>
  • complex<S>

对于常规的Express structs+complexclasss+complex,我都没有在代码库或基本库的代码库中找到一些东西。因此,我认为它确实是标准库复合体类型。

各种 heamer 文件中有几个 using namespace std,但不是全部。

我尝试用GCC 4.9和Clang 3.7编译此代码库。两者都给出了有关缺失类型的类似错误。是否有可能与较旧版本的GCC一起使用?我试图在所有需要的点上插入std::,但是我的印象是,如果我不能仅编译用于此类型的计算机的源头结帐,我做错了。

您可以使用选择性using...声明或键入混叠,仅引入您需要的std::成员。喜欢:

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

啊,是的,也许不是那么明显,所以值得一提。您现在std::endl吗?好吧,这是一个流操纵器,实际上是一个功能。这意味着人们也可以从其他名称空间中引入当前块函数

喜欢:

#include <cmath>
#include <cstdlib>
inline void dummy(float x) {
  float y=fabs(x); // Na'a.., fabs is imported into std namespace by <cmath>
  using std::itoa; // same is itoa, but we'll be using it
  char buff[128];
  itoa(42, buff, 10); // OK
}