Reputation: 27
I learned that in namespace "name decoration(mangling)" takes place so that it can be differentiated from other same identifiers which is in different namespace. Wiki: Name mangling
If then, Why "namespace scope" exists? I thought just 'name decoration' can solve all problem about name conflicting. Because in C, the reason for name conflicting is eventually that "different entities have same identifier". Name decoration can make names(identifiers) different from each other internally, so I think name decoration is what all we need.
Then, why C++ use 'namespace scope' concept? Just for to use unqualified name in namespace scope? I want to know if there is any reason.
Upvotes: 1
Views: 499
Reputation: 2447
Namespaces scope is very useful in programming for the following reasons:
foo::func()
and bar::func()
)sin()
, cos()
and sqrt()
function could be under namespace Math
.lexer
, but in different namespace scope like json::lexer
and xml::lexer
. Now, it gives clear understanding to the programmer to choose between them according to their language i.e, xml
or json
.An everyday example of namespace could be std
, stands for standard
, it contains every single functions and classes defined in the standard library, it also includes STL
classes like std::vector
, std::map
and std::string
.
NOTE: There's no concept of namespace scope in C
.
Upvotes: 1
Reputation: 1088
Namespace scope is a concept in the programming language that the developer sees, allowing them to organise their code effectively and understand it better. Name mangling is something the compiler does under the hood in order to implement namespaces (including namespace scope) in a way the linker can understand. In normal circumstances the developer doesn't need to think about name mangling, although its useful to know how it works.
Upvotes: 1