473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1349
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steffen Beyer | last post by:
Hi, my XSLT processes input documents which use various namespaces. Short example: XML input: <news:foo xmlns:news="http://some.example/news"/>
4
by: Daniel Mitchell | last post by:
I'm interested in computational physics and have defined an abstract base class to represent a simulation. Now I want to develop a GUI for setting the member data of classes derived from my base...
10
by: Alexander Stippler | last post by:
Hello, I have written a little library which consists of template functions and classes (99%) and two non-template classes. I'd appreciate very much if I could use the library by only including...
3
by: Eric Lilja | last post by:
Hello, I recently saw code like this: $ cat t.h namespace nc{ template<typename T> class Base { T hello; protected:
4
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
8
by: Dilip | last post by:
I was reading the C++ templates (Josuttis et al) book and came across an example that left me scratching my head.. there is a function template: template<typename T, int val> T addvalue(T...
5
by: Jim Langston | last post by:
I've been working on a project to map a MySQL database to a C++ class. Well, I actually got it to work, but some if it I just feel is exceptionally ugly. For example, in my operator<< override: ...
27
by: Cephalobus_alienus | last post by:
Hello, I know that macros are evil, but I recently came across a problem that I couldn't figure out how to solve with templates. I wanted to create a set of singleton event objects, and wrote...
0
by: anto.anish | last post by:
Hi , Since, i did not want to write all instantiations in Source file of all template methods for various different datatypes that my client might use, Instead, i choose to write implementation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.