How do I find out how a namespace is dirty?

Consider the following small piece of code:

// 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??
}

This compiles, so obviously some of the recursively included header files have

using namespace std;

somewhere. How would you know where this offensive line of code is?

Just using grepheader files for all files will not work, because this statement is often used inside a function where it is safe and will not pollute the rest of the code.

+3
source share
4 answers

grep can be useful anyway. Do a search for "^ using namespace". There is a pretty good chance that it will be indented inside the function, but not outside it ...

+6

(-E ), q #line.

+5

string s; #include, , . , . #include ..

, .

+4
$ g++ -E souce.cpp | less

/using , ,

# <file_name> <line_number>

, , - , .

+2

Source: https://habr.com/ru/post/1740365/


All Articles