Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to warn me Global Namespace Pollutions?

Does gcc (or any other compiler) have an option to warn me global namespace pollutions?

I tried to compile a sample application suite, but it turned out quite a few of header files of that samples are stricken with global namespace pollutions (using namespace in header files), so that Mac OS X global struct Rect becomes ambiguous. I also tried to change the order of #include to resolve the system's Rect symbol before the minefield of using namespace, but that caused some symbols end up not resolved.

My earlier search using Google failed, bad premonition though, if that kind of warning option exists, please give me info.

like image 508
Shigerello Avatar asked Apr 28 '11 05:04

Shigerello


People also ask

How do namespace help in preventing pollution of global namespace?

Technically, namespace pollution is simply leaving your symbols in a namespace where they shouldn't really be. This doesn't necessarily lead to clashes but it makes it more likely. both of those functions are made available to the linker.

What is namespace pollution?

Namespace pollution is a lot like pollution in general. It means that something is misplaced. In programming that means that code that should really live in separate namespaces is added to a common namespace (in some cases the global namespace).


1 Answers

I don't know about gcc, but CLang has it:

def warn_using_directive_in_header : Warning<
  "using namespace directive in global context in header">,
  InGroup<HeaderHygiene>, DefaultIgnore;

You can activate it directly using -Wusing-directive-in-header or via the group -Wheader-hygiene (it is, for now, the sole member of the group).

It's been thoroughly reviewed on the mailing list and normally does not warn about regular cases.

like image 112
Matthieu M. Avatar answered Oct 05 '22 05:10

Matthieu M.