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();
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.