user8063157
user8063157

Reputation: 285

Restrict namespace pollution in C++ Header

In our team we want to Restrict practice of adding "using namespace" in header at global scope as that is not a good practice and often leads to name collision and namespace pollution.

Is there a way in gcc we can restrict this practice of adding using namespace in header at compile time?

I mean to get some kind of warning when the compiler sees at global scope in header "using namespace".

Thanks

Upvotes: 4

Views: 922

Answers (2)

Johann Gerell
Johann Gerell

Reputation: 25581

Just gonna throw this one out there, as it is a very viable alternative in this specific case:

In 10 minutes you can likely in any script language of your choice write something that traverses your source tree and scans header files for "using namespace". Run this script automatically on your "nightly build" and fail the build if any such usage is found.

Upvotes: 1

lubgr
lubgr

Reputation: 38267

You can use clang-tidy together with the google build using namespace check, which should complain on these using directives.

Upvotes: 8

Related Questions