468,509 Members | 1,439 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,509 developers. It's quick & easy.

Avoiding ugly template pedantry

I have a typedef in a base class, and I want it to effortlessly filter
through to the derived class. Here's a quick example:
class Base {
public:

typedef unsigned SpecialType;

};
class Derived : public Base {
public:

SpecialType obj;

};
int main()
{
Derived obj;

obj.obj = 7;
}
This works perfectly. However, if I bring templates into the equation,
the following doesn't compile:

template<class T>
class Base {
public:

typedef unsigned SpecialType;

};

template<class T>
class Derived : public Base<T{
public:

SpecialType obj;

};
int main()
{
Derived<intobj;

obj.obj = 7;
}
Instead, I have to write:
template<class T>
class Base {
public:

typedef unsigned SpecialType;

};

template<class T>
class Derived : public Base<T{
public:

typename Base<T>::SpecialType obj;

};
int main()
{
Derived<intobj;

obj.obj = 7;
}
Things get really ugly when I've got a lot of templates; here's the
signature of a function I wrote recently:

template<class T, class U>
const typename Arb<T,U>::SpecialType* Arb<T,U>::Func();
Is there any way nice way to avoid this ugliness? Are there any proposals
(other than templated namespaces) to simplify this stuff? For instance,
in the same fashion as "Koenig look-up", I think the previous function
declaration should be able to be written as:

template<class T, class U>
const SpecialType* Arb<T,U>::Func();
Or perhaps even:
const SpecialType *Arb::Func();
I'm writing code at the moment which is VERY heavy on nested templates,
and things are getting quite ugly. I'm running out of horizontal screen
space very quickly.

--

Frederick Gotham
Jul 4 '06 #1
5 1174
* Frederick Gotham:
[snip]
>
Instead, I have to write:

template<class T>
class Base {
public:

typedef unsigned SpecialType;

};

template<class T>
class Derived : public Base<T{
public:

typename Base<T>::SpecialType obj;

};
How about

template<class T>
class Derived : public Base<T{
public:
typedef Base<TSuper; // Just in case lots of templ.args.
typedef typename Super::SpecialType SpecialType;
SpecialType obj;
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 4 '06 #2

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:5j*******************@news.indigo.ie...
>I have a typedef in a base class, and I want it to effortlessly filter
through to the derived class. Here's a quick example:
class Base {
public:

typedef unsigned SpecialType;

};
class Derived : public Base {
public:

SpecialType obj;

};
int main()
{
Derived obj;

obj.obj = 7;
}
This works perfectly. However, if I bring templates into the equation,
the following doesn't compile:

template<class T>
class Base {
public:

typedef unsigned SpecialType;

};

template<class T>
class Derived : public Base<T{
public:

SpecialType obj;

};
int main()
{
Derived<intobj;

obj.obj = 7;
}
The above code worked fine for me.
>

Instead, I have to write:
template<class T>
class Base {
public:

typedef unsigned SpecialType;

};

template<class T>
class Derived : public Base<T{
public:

typename Base<T>::SpecialType obj;

};
int main()
{
Derived<intobj;

obj.obj = 7;
}
Things get really ugly when I've got a lot of templates; here's the
signature of a function I wrote recently:

template<class T, class U>
const typename Arb<T,U>::SpecialType* Arb<T,U>::Func();
Is there any way nice way to avoid this ugliness? Are there any proposals
(other than templated namespaces) to simplify this stuff? For instance,
in the same fashion as "Koenig look-up", I think the previous function
declaration should be able to be written as:

template<class T, class U>
const SpecialType* Arb<T,U>::Func();
Or perhaps even:
const SpecialType *Arb::Func();
I'm writing code at the moment which is VERY heavy on nested templates,
and things are getting quite ugly. I'm running out of horizontal screen
space very quickly.

--

Frederick Gotham
Why doesn't it compile? It works OK for me on VC7. What message do you get?
And what compiler are you using?

Cy
Jul 4 '06 #3
* Cy Edmunds:
>
Why doesn't it compile? It works OK for me on VC7. What message do you get?
And what compiler are you using?
I'm guessing g++ for Windows; that was the basis of my reply elsethread.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 4 '06 #4
Cy Edmunds posted:
>template<class T>
class Derived : public Base<T{
public:

SpecialType obj;

};
int main()
{
Derived<intobj;

obj.obj = 7;
}

The above code worked fine for me.

I'm using g++ in combination with Dev-C++, and I get a compile error. The
compiler even gives me a hint:

"Perhaps you meant: typename Base<T>::SpecialType"
According to the Standard, should the code compile? I'm going to do two
things:

(1) Update g++
(2) Ask over on comp.std.c++ if it should compile
--

Frederick Gotham
Jul 4 '06 #5
Frederick Gotham wrote:
Cy Edmunds posted:

>>>template<class T>
class Derived : public Base<T{
public:

SpecialType obj;

};
int main()
{
Derived<intobj;

obj.obj = 7;
}

The above code worked fine for me.

I'm using g++ in combination with Dev-C++, and I get a compile error. The
compiler even gives me a hint:

"Perhaps you meant: typename Base<T>::SpecialType"
According to the Standard, should the code compile? I'm going to do two
things:

(1) Update g++
(2) Ask over on comp.std.c++ if it should compile
You need only do that if no one can answer you here. But of course,
typename is required wherever you name a type that is a dependent name,
as in the examples you presented. Some older compilers are permissive
about this, since they don't support "two phase name lookup". IIRC GCC
3.4 and later supports it.

Tom
Jul 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Daniel Mitchell | last post: by
10 posts views Thread by Alexander Stippler | last post: by
3 posts views Thread by Eric Lilja | last post: by
4 posts views Thread by Frank-René Schäfer | last post: by
8 posts views Thread by Dilip | last post: by
5 posts views Thread by Jim Langston | last post: by
27 posts views Thread by Cephalobus_alienus | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.