Изменения

Перейти к: навигация, поиск

Участник:Yulya3102/Плюсы2сем

1335 байт добавлено, 23:48, 19 января 2013
anonymous namespaces
=== Concepts ===
=== anonymous namespaces ===
You can declare an unnamed namespace as a superior alternative to the use of global static variable declarations.
namespace { namespace-body }
 
An unnamed-namespace-definition having the syntax shown above behaves as if it were replaced by:
namespace unique { namespace-body }
using namespace unique;
 
Each unnamed namespace has an identifier, assigned and maintained by the program and represented here by unique, that differs from all other identifiers in the entire program. For example:
// unnamed_namespaces.cpp
// C2872 expected
namespace { int i; } // unique::i
void f() { i++; } // unique::i++
namespace A {
namespace {
int i; // A::unique::i
int j; // A::unique::j
}
}
using namespace A;
void h()
{
i++; // C2872: unique::i or A::unique::i
A::i++; // C2872: A::i undefined
j++; // A::unique::j++
}
 
Unnamed namespaces are a superior replacement for the static declaration of variables. They allow variables and functions to be visible within an entire translation unit, yet not visible externally. Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.
355
правок

Навигация