I looked up on Wiki for the keyword typename (http://en.wikipedia.org/wiki/Typename) and it gives an example which typename is required before T::bar
template <typename T>
void foo(const T& t)
{
// declares a pointer to an object of type T::bar
T::bar * p;
}
struct StructWithBarAsType {
typedef int bar;
};
int main() {
StructWithBarAsType x;
foo(x);
}
Why doesn't C++ inspect StructWithBarAsType and find that T::bar is actually a type when generating code for foo(x)? Is it not possible or just an efficiency consideration?
it assumes that
T::bar
is a static member unlesstypename
is specified beforehand. afaict there is no technical limitation that would prevent compilers from being able to figure out during instantiation whether or notT::bar
is a type, but to keep things a bit easier for compiler implementors, the decision was made. It can make more of a difference than than one might think, consideris this a variable
c
of typeT::bar<a,b>
, or is it a less than, comma, greater than?T::bar < a
followed byb > c
? C++ template syntax leaves a lot to be desired.