What I read in the C++ standard about injected class names contradicts (as I see it) with the behavior of a sample program I will present shortly. Here's what I read:
From 3.4 (paragraph 3)
The injected-class-name of a class (clause 9) is also considered to be a member of that class for the purposes of name hiding and lookup.
From 9 (paragraph 2)
A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.
From these I understand that the following is a well-formed translation unit and it compiles successfully.
#include <vector>
class X: std::vector<int>
{
vector mem;
};
However, I would suppose that the following should have produced an error, but it doesn't
#include <vector>
class X: std::vector<int>, std::vector<char>
{
vector mem; //compiles OK... mem is apparently std::vector<int>
};
Since the name vector
is injected into both std::vector<int>
and std::vector<char>
as as if a public member name, then it should be inherited by X and therefore the name vector
in X
should be ambiguous. Am I missing something?
P.S. I am using MSVC9.0
X
in your example? I.e. is the injected-class-name notX
rather thanvector
? I think that is the case, and if so the quotes are irrelevant to the code, but still the behavior of that compiler does not seem to be correct either there is no reason to preferstd::vector<int>
overstd::vector<char>
and that means that there should be an ambiguity error being triggered. – CandiX
yes, butvector
is injected as a public name in both the bases... So it should be visible(and ambiguous) insideX
as well - as in the first example – Dominancestd::vector
, A class-name is inserted into the scope in which it is declared immediately after the class-name is seen.: std namespace; The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name.: std::vector template. I still think that those quotes are not related to the particular code example. – Candi