如何找出命名空间如何被污染?

Fra*_*ank 3 c++ unix namespaces header-files

考虑以下一小段代码:

// all of these include other headers, lots of code:
#include "myheader1.h"
#include "myheader2.h"
#include <string>

void foo() {
  string s("hello world"); // oh no, why does this compile??
}
Run Code Online (Sandbox Code Playgroud)

这个编译,显然一些递归包含的头文件有一个

using namespace std;
Run Code Online (Sandbox Code Playgroud)

某处.您将如何找到违规代码行的位置?

只是grep在所有头文件上使用都不会真正起作用,因为该语句通常函数内部使用,它是安全的并且不会污染其余的代码.

Jer*_*fin 6

无论如何,grep可能会有用.搜索"^ using namespace".在一个函数里面有一个非常公平的机会它会缩进,但在它之外它不会......


APr*_*mer 5

编译器通常可以给出预处理的输出(-E是常见的),它也可以在q #line行中指示真正的源线.