Scope of structure declared/defined within another

2019-07-05 13:14发布

问题:

Reading from Nested structures, I realized that structure declared within another structure is scoped the same as the containing structure. I thought it is only scoped within the containing structure. I got this impression from this link. It says there are 4 namespaces and one of them is members of a structure. I "logically" inferred that inner structures are only scoped within the outer one.

Can anyone provide a reference to the standard as to exactly how the scope rule works in this case? And any rationale for allowing inner struct to be visible outside the containing structure? If it is visible, why not just declare the struct outside...

Question 2: For terminology, when I provide the members of a structure, say

struct out{
    int a, b;
    char c, d;
    struct in{
        int a, b;
    }e;
};

Am I providing a definition for both struct out and struct in; OR am I providing a declaration for both? I understand the difference for functions and primitive data types but not really clear here for struct s.

EDIT: Useful link I just found on SO: Nested structures in C and C++.

But there it does not provide any rationale. And now I doubt if there is one for C...

回答1:

For the C99 draft standard the rules for scope are covered in 6.2.1 Scopes of identifiers and in paragraph 7 says(emphasis mine):

Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.

The scope ends following the rules laid out in paragraph 4:

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. [...]

As for the rationale, it probably has to do with the fact that unlike C++, in C we don't have the scope resolution operator(::).

As for question 2 you are providing a definition and a declaration in your example, this would be an example of just a declaration:

struct out ;

Also see: What is the difference between a definition and a declaration?.

For a list of all the incompatibilities between C and C++ you can check out Incompatibilities Between ISO C and ISO C++ it covers this subject here.



回答2:

In C, there isn't any "hiding implementation details," that you happen to define the structure tag textually inside the outer structure is incidental. (No namespaces, no ::)

In C++, encapsulation (hiding) is very relevant, it considers the internal structure defining a type internal to out (in its namespace), not to be used outside unless specifically called for with ::. Classes and structures in C++ are almost the same thing, so that it is struct, not class, is incidental here.

One of the fun ways in which C and C++ differ.



标签: c struct scope