Reputation: 16765
What is the difference between:
namespace {
// code
} // anonymous namespace
and
{
// code
}
I understand that:
Do I miss something?
Upvotes: 1
Views: 279
Reputation: 27385
namespace {}
creates a namespace (a space for names) that changes how the declarations inside will be found by the compiler (in this case, it is an anonymous namespace, meaning that symbols declared inside are accessible only in the current compilation unit).
{}
creates a scope (a block of executable code) that isolates the statements inside, and changes how the code within the block will be executed.
The first matters to the compiler, the second matters at execution. They are completely different things.
Upvotes: 2
Reputation: 234855
There are enormous differences.
Anything in an anonymous namespace is only visible to that compilation unit. (It's somewhat similar to one particular use of static
). You can't put statements into anonymous namespaces.
{ }
is a scoping block. It can contain statements.
Upvotes: 4
Reputation: 40150
There is a big difference: what is defined in an anonymous namespace still exists outside, although what's created inside a block dies with it ending.
Upvotes: 1