C++ for Embedded · Interview question

Why is using namespace in a header file a bad idea?

A strong answer

Because a header is textually pasted into every translation unit that includes it, a using namespace foo; at header scope dumps all of foo's names into every one of those files, and transitively into anything that includes them. That re-creates exactly the global name-collision problem namespaces exist to prevent, except now it's invisible and happens in files that never asked for it. A name you pulled in can silently shadow or conflict with an unrelated symbol three includes away, producing baffling errors far from the header. The discipline is: never put using namespace (or even broad using directives) at file scope in a header. Inside a .cpp or a function body it's bounded and acceptable; targeted using foo::Bar; is better still; and namespace aliases handle long names without importing everything.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Namespaces & Code Organization

Group related names to avoid collisions, replace C's prefix conventions, and structure firmware into headers and translation units that compile fast and read clearly.

More Namespaces & Code Organization questions

Browse all 472 interview questions
Why is using namespace in a header file a bad idea? | EmbeddedPrep.io