C++ for Embedded · Interview question

What's the difference between static and an anonymous namespace for file-private symbols?

A strong answer

Both give internal linkage, the symbol is private to its translation unit and invisible to the linker from other files. For functions and variables they're equivalent, but the anonymous (unnamed) namespace is more capable and is the modern C++ preference for two reasons: it can make types file-private (a struct or class inside an unnamed namespace has internal linkage, which static can't express, static applies to objects and functions, not type definitions), and it groups multiple internal-linkage declarations cleanly under one namespace { ... } block. So in C++ you reach for the anonymous namespace to say "these are implementation details of this .cpp, don't link to them elsewhere," including for helper types, while static on a local function still works and is fine for a one-off.

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
What's the difference between static and an anonymous namespace for file-private symbols? | EmbeddedPrep.io