-
Qualified typedef
Hello all,
I recently came across an example of a class-qualified typedef being used like this:
Code:
#include <iostream>
using namespace std;
class X {};
typedef X* C;
template<class T> class A
{
public:
typedef int C;
typename A::C d; // <---HERE
};
int main()
{
A<X> a;
a.d = 23;
cout << a.d;
}
(It was here: Help - IBM Mac OS X Compilers)
I was a bit surprised by this, since I have never seen a class typedef being used like this before. Can a typedef be qualified in the way any other name can?
Also, is there any reason to do this other than for illustrative purposes, as in this example showing how typename is used? Given that the class typedef hides the global typedef, I'd have written C d; rather than typename A::C d;!
Thanks.
-
This is for mostly for illustration purposes. But when you get into nasty template programming, this is quite useful.
For example, we can specialize class A and depending on the type T, the typedef contained inside may be a completely different type!
-
Typedef names are names like any others and thus can be qualified.
-
But in that case, wouldn't C d; in class scope refer to the right type for the particular specialization in questiion?
-
Yeah it would. So it's an illustration rather than a good example.
It's different outside the class, of course.
-
OK.
Thanks to both of you.