I
IR
Hi,
I've been trying to do the following (which doesn't compile) :
template<class T, class F = Example<T> >
struct Example
{
F foo();
};
which gives me a "recursive type or function dependency context too
complex" error (I don't really understand _why_).
Trying to change the declaration to :
template<class T, class F = Example<T,F> >
struct Example
{
F foo();
};
just leads me to a "'F' : undeclared identifier" error (which I
fully understand, contrary to the previous error).
The goal I'm trying to achieve is to be able to write both
typedef Example<SomeClass> SomeExample;
and
struct OtherExample : public Example<OtherClass,OtherExample>
{
//... whatever
};
SomeExample::foo should have a return type of Example<SomeClass>
(aka. SomeExample due to the typedef), while OtherExample::foo
should have a return type of OtherExample.
I've already done things like these, but without template default
argument, as the base template was then designed to be inherited
from (ctors protected, ...). But in this case, the base template
(Example) can be used "standalone".
The scary part with this one, is that without Example's default
template argument (F), I wouldn't be able to write
typedef Example<SomeClass, Example<SomeClass, ... > > SomeExample;
due to the recursive nature of the declaration.
So I guess that I must really be doing something wrong.
The only solution I've come up with is something along the lines:
class SelfType {};
template<class T, class F = SelfType>
struct Example
{
private:
template<class U> struct helper { typedef U Type; };
template<> struct helper<SelfType> { typedef Example<T> Type; };
typedef typename helper<F>::Type FinalType;
public:
FinalType foo();
};
This seems to work as far as I tested it, but I'm not satisfied
mainly due to the "scary part" I mentionned (which leads me to
believe I'm kinda playing with Evil).
Do you think I am missing something?
I've been trying to do the following (which doesn't compile) :
template<class T, class F = Example<T> >
struct Example
{
F foo();
};
which gives me a "recursive type or function dependency context too
complex" error (I don't really understand _why_).
Trying to change the declaration to :
template<class T, class F = Example<T,F> >
struct Example
{
F foo();
};
just leads me to a "'F' : undeclared identifier" error (which I
fully understand, contrary to the previous error).
The goal I'm trying to achieve is to be able to write both
typedef Example<SomeClass> SomeExample;
and
struct OtherExample : public Example<OtherClass,OtherExample>
{
//... whatever
};
SomeExample::foo should have a return type of Example<SomeClass>
(aka. SomeExample due to the typedef), while OtherExample::foo
should have a return type of OtherExample.
I've already done things like these, but without template default
argument, as the base template was then designed to be inherited
from (ctors protected, ...). But in this case, the base template
(Example) can be used "standalone".
The scary part with this one, is that without Example's default
template argument (F), I wouldn't be able to write
typedef Example<SomeClass, Example<SomeClass, ... > > SomeExample;
due to the recursive nature of the declaration.
So I guess that I must really be doing something wrong.
The only solution I've come up with is something along the lines:
class SelfType {};
template<class T, class F = SelfType>
struct Example
{
private:
template<class U> struct helper { typedef U Type; };
template<> struct helper<SelfType> { typedef Example<T> Type; };
typedef typename helper<F>::Type FinalType;
public:
FinalType foo();
};
This seems to work as far as I tested it, but I'm not satisfied
mainly due to the "scary part" I mentionned (which leads me to
believe I'm kinda playing with Evil).
Do you think I am missing something?