In C++14 standard, [basic.scope.hiding], paragraph 2 (3.3.10.2), says:
A class name or enumeration name can be hidden by the name of a variable, data member, function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data member, function, or enumerator are declared in the same scope (in any order) with the same name, the class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is visible.
I'm disturbed by the wording "same scope". Indeed, in the following snippet of code, what are the cases where the class C
and the variable C
are declared in the same scope?
namespace case_1 {
int C;
class C;
// Here C refers to variable
}
namespace case_2 {
class C;
namespace nested {
int C;
// Here C refers to variable
}
}
namespace case_3 {
int C;
namespace nested {
class C;
// Here C refers to class
}
}
namespace case_4 {
enum Enum { A, B, C };
class C;
// Here C refers to enumerator
}
Hypothesis 1: "same scope" means "same block"
If we adhere to this hypothesis, the case 1 should be concerned by the rule 3.3.10.2. What about case 2? I guess it is covered by rule 3.3.10.1:
A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class.
Also, this hypothesis explains well the case 3 where the class name hides the variable name (and not the inverse) but can't explain the case 4. Indeed, C
enumerator is declared in a different block than C
class but the class is still hided.
Hypothesis 2: "declared in the same scope" means "have exactly the same scope"
If that hypothesis is true, no cases described in my code are concerned by the rule because it impossible for two name to have the exact same scope. Even the variable in case_1
has a different scope that the class. Indeed, the scope of the variable name starts after its declaration so before the class name.
Hypothesis 3: "declared in the same scope" means "one of the name is declared inside the scope of the other"
If this hypothesis is true, all the cases describe above should be covered by the rule 3.3.10.2. Indeed, in case_1
and case_3
, C
class is declared in the scope of C
variable; in case_2
, C
variable is declared in the scope of C
class; and, in case_4
, C
class is declared in the scope of C
enumerator. However, the case_3
doesn't follow the rule because it is the variable who should "win" and stay visible.
As you can see, each of my hypothesis have a drawback and I really don't understand what the standard exactly means by "same scope" in that paragraph.