By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

Avoiding ugly template pedantry

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
* 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

P: n/a

"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

P: n/a
* 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

P: n/a
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

P: n/a
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.