class A
{
static int iterator;
class iterator
{
[...]
};
[...]
};
I (think I) understand the reason why typename
is needed here:
template <class T>
void foo() {
typename T::iterator* iter;
[...]
}
but I don't understand the reason why typename
is not needed here:
void foo() {
A::iterator* iter;
[...]
}
Can anyone explain?
The reason why the compiler does not have a problem with the latter, I found to be answered well in a comment:
in the case of A::iterator
I don't see why the compiler wouldn't confuse it with the static int iterator
? - xcrypt
@xcrypt because it knows what both A::iterator
s are and can pick which one depending on how it is used – Seth Carnegie
The reason why the compiler needs typename
before the qualified dependent names, is in my opinion answered very well in the accepted answer by Kerrek SB. Be sure to also read the comments on that answer, especially this one by iammilind:
"T::A * x;, this expression can be true for both cases where T::A is a type and T::A is a value. If A is a type, then it will result in pointer declaration; if A is a value, then it will result in multiplication. Thus a single template will have different meaning for 2 different types, which is not acceptable."
The typename keyword is needed whenever a type name depends on a template parameter, (so the compiler can 'know' the semantics of an identifier (type or value) without having a full symbol table at the first pass).
" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.
A dependent name is a name that depends on the type or the value of a template parameter. For example: template<class T> class U : A<T> { typename T::B x; void f(A<T>& y) { *y++; } }; The dependent names in this example are the base class A<T> , the type name T::B , and the variable y .
A dependent name is essentially a name that depends on a template parameter. A dependent name can be a type, a non-type, or a template parameter. To express that a dependent name stands for a type or a template, you have to use the keywords typename or template .
A name in C++ can pertain to three different tiers of entities: Types, values, and templates.
struct Foo
{
typedef int A; // type
static double B; // value
template <typename T> struct C; // template
};
The three names Foo::A
, Foo::B
and Foo::C
are examples of all three different tiers.
In the above example, Foo
is a complete type, and so the compiler knows already what Foo::A
etc. refer to. But now imagine this:
template <typename T> struct Bar
{
T::A x;
};
Now we are in trouble: what is T::A
? if T = Foo
, then T::A = int
, which is a type, and all is well. But when T = struct { static char A; };
, then T::A
is a value, which doesn't make sense.
Therefore, the compiler demands that you tell it what T::A
and T::B
and T::C
are supposed to be. If you say nothing, it is assumed to be a value. If you say typename
, it is a typename, and if you say template
, it is a template:
template <typename T> struct Bar
{
typename T::A x; // ah, good, decreed typename
void foo()
{
int a = T::B; // assumed value, OK
T::template C<int> z; // decreed template
z.gobble(a * x);
}
};
Secondary checks such as whether T::B
is convertible to int
, whether a
and x
can be multiplied, and whether C<int>
really has a member function gobble
are all postponed until you actually instantiate the template. But the specification whether a name denotes a value, a type or a template is fundamental to the syntactic correctness of the code and must be provided right there during the template definition.
Because in the nontemplate foo, you are doing a valid multiplication operation (assuming you declared iter
before that use). Try to omit the star, and you will get a compiler error. The int hides the class.
The typename keyword does not prevent that hiding. Only gcc implements it incorrectly to do so. So if you try to instantiate your function template with A
as the type, then you will get a compile error, because the name specified afteer typenake will refer to a nontype on any standard conformant compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With