Uniqueness of structure names

Although the name of the structure must be unique in the set of structures in the namespace, such a name can be "shared" with variables and functions. For example, the following code compiles just fine:

// Code 1 struct h{}; int h{ 8 }; 

Similarly, there are no collisions:

 // Code 2 struct h{}; void h(){} 

1) What was the reason to use this name?

In addition, if we throw patterns into the mix, we have strange situations. Code

 // Code 3 template< class H > void h(){} struct h{}; template< class H > struct j{}; void j(){} 

compiles but the following code does not work:

 // Code 4 struct h{}; template< class H > void h(){} void j(){} template< class H > struct j{}; 

2) Why is the argument allowing code 2 not good enough to allow code 4? I do not ask about the rules in the standard. I ask about the reason for these rules.

+6
source share
1 answer

1) What was the reason to use this name?

According to “Design and Evolution of C ++,” Bjarne Stroustrup , section 2.8.2, “Structure Tags versus Type Names,” this function was introduced to ensure compatibility with C. The C language requires the use of the struct keyword to indicate structure (to indicate typedef no keyword required). Then you can use the same identifier to declare a structure and either a function or a variable:

 struct X { int m; }; void X(void); X(); // call X struct X x; // create a new object of type X 

Significant syntactic simplification for users was introduced in C ++ due to some additional work for developers and some compatibility problems with C. [...] In the context of C with classes [dyp: C ++ predecessor], this annoyed me for some time because he created custom types of second-class citizens syntactically.

[...]

The real need to solve this problem arose because some standard UNIX header files, especially stat.h , rely on struct and a variable or function with the same name.

- D & E 2.8.2


2) Why is the argument allowing code 2 not good enough to allow code 4?

First, I’ll put the C ++ 11 code:

A class template must not have the same name as any other template, class, function, variable, enumeration, enumerator, namespace or type in the same scope (3.3), except as specified in (14.5.5 [dyp: class template partial specialization]). In addition, a function template can be overloaded either (without a template) with the same name or using other function templates with the same name (14.8.3 [dyp: this refers to overloading]), the name of the template declared in the namespace area or The scope class must be unique in this scope.

- C ++ 11 International Standard [temp] p5

According to my interpretation of this passage, codes 3 and 4 are illegal. clang ++ rejects them, but accepts the first part of code 3:

 template< class H > void h(){} struct h{}; 

Which, according to [temp] p5, should also be illegal.

Considering all these examples (3, 4 and beginning 3) as illegal, I believe that the rationale is that in these cases a compatibility exception is not required: C has no templates.

+3
source

All Articles