F
Frederick Gotham
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<int> obj;
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<int> obj;
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.
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<int> obj;
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<int> obj;
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.