1
votes

C(ISO/IEC 9899:2011) has no concept of namespace, when referring global variable's scope, the standard use file scope:

Every other identifier has scope determined by the placement of its declaration (in a declarator or type specifier). If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit. If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. If the declarator or type specifier that declares the identifier appears within the list of parameter declarations in a function prototype (not part of a function definition), the identifier has function prototype scope, which terminates at the end of the function declarator. If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

However, in c++(at least ISO/IEC 14882:2011), file scope is never used and use namespace scope instead:

The declarative region of a namespace-definition is its namespace-body. Entities declared in a namespace-body are said to be members of the namespace, and names introduced by these declarations into the declarative region of the namespace are said to be member names of the namespace. A namespace member name has namespace scope. Its potential scope includes its namespace from the name's point of declaration onwards; and for each using-directive ([namespace.udir]) that nominates the member's namespace, the member's potential scope includes that portion of the potential scope of the using-directive that follows the member's point of declaration.

As we know, c++ is inspired a lot from C, so many definitions of terms also derive from c, though they are virtually two different languages now. IMO, there should be a reason that c++ use namespace scope instead of file scope, but I don't know. What's behind it?

2
Because C as a very very poor concept of modularity and C++ tried to correct it. Please forget about trying to relate C concepts to C++ concepts, now you will benefit from considering as really (not virtually) two different languages.Jean-Baptiste Yunès
They are not "virtually two different languages". They are two different languages, full stop, end of story.n. 1.8e9-where's-my-share m.
FWIW, your function asks like 3 different things and I'm not sure what you actually want to know. The title use file scope, your body says function scope (which doesn't exist in your quote) and then you ask why use namespaces, what is the actual question?NathanOliver

2 Answers

5
votes

The C++ standard doesn't mention file scope because it isn't trying to explain things in C terms. It defines and uses its own terms. In this case, it is under the definition of namespaces:

[basic.namespace]

2 The outermost declarative region of a translation unit is a namespace; see [basic.scope.namespace].

This declarative region, in C++ terms, is the equivalent of C's file scope. The C++ standard uses these definitions because it intends to support namespace declarations, that allow us to partition this scope into smaller pieces.

C has no such feature, so it speaks only of things at file scope as the its outermost scope, because said definition is enough.

2
votes

You're asking about file scope, not function scope.

The reason that C++ instead talks about namespace scope, is that it has namespaces. C doesn't. So of course C wouldn't have namespace scope.

C and C++ are different languages. C++ doesn't change things to "not be C", it changes things because its designers consider the changes to make the language inherently "better", either directly, or by consequence of the existence of some C++-only feature that in turn makes the language "better" than C.