unnecessarily explicit typedef in complicated templates?

P

poliklosio

If you look at the following code:

#include <iostream>
using namespace std;

struct FormatType
{
typedef char char_type;
};

template<class CharT>
struct A
{
typedef CharT ggg;
};

template<class FormatT>
struct B:public A<typename FormatT::char_type>
{
//this has to be explicit... what a shame
typename A<typename FormatT::char_type>::ggg g;
};

int main() {
B<FormatType> b;
cout<<b.g<<endl;
return 0;
}




Why does the line
typename A<typename FormatT::char_type>::ggg g;
have to be so explicit, instead of just saying
ggg g;
?

I use G++ 4.4.1

I thought I could avoid repeating some typedefs (in multiple classes
similar to B above) by inheriting from a class which defines them
(class A above), but apparently that requires writing them very
verbosely, which completely defeats the advantages. Why?
 
A

Alf P. Steinbach /Usenet

* poliklosio, on 28.06.2010 20:47:
If you look at the following code:

#include<iostream>
using namespace std;

struct FormatType
{
typedef char char_type;
};

template<class CharT>
struct A
{
typedef CharT ggg;
};

template<class FormatT>
struct B:public A<typename FormatT::char_type>
{
//this has to be explicit... what a shame
typename A<typename FormatT::char_type>::ggg g;
};

int main() {
B<FormatType> b;
cout<<b.g<<endl;
return 0;
}




Why does the line
typename A<typename FormatT::char_type>::ggg g;
have to be so explicit, instead of just saying
ggg g;
?

I use G++ 4.4.1

'ggg' a dependent name, that is, what it means could depend on a template parameter.


I thought I could avoid repeating some typedefs (in multiple classes
similar to B above) by inheriting from a class which defines them
(class A above), but apparently that requires writing them very
verbosely, which completely defeats the advantages. Why?

Well, thanks for bringing issue that back to my attention. A workaround is
sorely needed. I found that the following compiles with g++ 4.3.1 and with
Comeau Online:


<code>
#include <iostream>
using namespace std;

struct FormatType
{
typedef char char_type;
};

template<class CharT>
struct A
{
typedef CharT ggg;
};

template<class FormatT>
struct B:public A<typename FormatT::char_type>
{
typedef typename B::ggg Ggg;
Ggg g;
};

int main() {
B<FormatType> b;
cout<<b.g<<endl;
return 0;
}
</code>


g++ (and also MSVC) was happy with 'ggg' instead of 'Ggg', but Comeau then
complained, I don't know why.

I suspect that Comeau, for once, got it wrong, but the rules here are subtle and
my take it on it is, whatever works :), plus Scott Meyers' advice to compile
with at least two different compilers.


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top