Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > CRTP question

Reply
Thread Tools

CRTP question

 
 
michael.alexeev@qwest.com
Guest
Posts: n/a
 
      04-17-2006
Hi all,

Consider the following program:

template <class T>
struct Traits{
typedef typename T::type_t type_t; //line 3
};

template <class Derived>
struct Base{
typedef typename Traits<Derived>::type_t type_t; // line 8
type_t foo() {
Derived& self = static_cast<Derived&>(*this);
return self.bar();
}

};

template<typename T>
struct Derived : public Base<Derived<T> >{
typedef T type_t;
type_t bar() {
return type_t();
}
};

int main(){
Derived<int> d;
return 0;
}

Compiler (gcc 4.0.2) complains with the following message:

t.cxx: In instantiation of Traits<Derived<int> >:
t.cxx:8: instantiated from Base<Derived<int> >
t.cxx:17: instantiated from Derived<int>
t.cxx:25: instantiated from here
t.cxx:3: error: no type named type_t in struct Derived<int>

I was under the impresion that declarations in the Base class template
are instantiated when the Derived class is declared which means that
Derived::type_t is accessable. Looks like it's not a case.

Any help/walkaround is greatly appreciated.

Mike

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-17-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Consider the following program:
>
> template <class T>
> struct Traits{
> typedef typename T::type_t type_t; //line 3
> };
>
> template <class Derived>
> struct Base{
> typedef typename Traits<Derived>::type_t type_t; // line 8
> type_t foo() {
> Derived& self = static_cast<Derived&>(*this);
> return self.bar();
> }
>
> };
>
> template<typename T>
> struct Derived : public Base<Derived<T> >{
> typedef T type_t;
> type_t bar() {
> return type_t();
> }
> };
>
> int main(){
> Derived<int> d;
> return 0;
> }
>
> Compiler (gcc 4.0.2) complains with the following message:
>
> t.cxx: In instantiation of Traits<Derived<int> >:
> t.cxx:8: instantiated from Base<Derived<int> >
> t.cxx:17: instantiated from Derived<int>
> t.cxx:25: instantiated from here
> t.cxx:3: error: no type named type_t in struct Derived<int>
>
> I was under the impresion that declarations in the Base class template
> are instantiated when the Derived class is declared which means that
> Derived::type_t is accessable. Looks like it's not a case.


The problem is that by the time it gets to use 'Derived<int>::type_t',
during instantiating of Derived<int>::Base<Derived<int> >, the inner
contents of 'Derived<int>' haven't been reached yet, so none of its
members are known.

> Any help/walkaround is greatly appreciated.


You could try specialising 'Traits' this way:

template<class T> class Derived; // forward-delcaration
template<class T> class Traits<Derived<T> > { typedef T type_t; };

....since you know that 'type_t' is going to be the same as T. And then
declare your Base, and so forth.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
michael.alexeev@qwest.com
Guest
Posts: n/a
 
      04-17-2006
Thanks Victor,

That's did the trick.

Mike

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRTP Noah Roberts C++ 6 01-15-2007 09:39 AM
CRTP and Factories alexander.stippler@uni-ulm.de C++ 2 07-25-2006 02:35 PM
CRTP question fabioppp C++ 2 04-15-2005 04:43 PM
HSSI & cRTP Arthur Lashin Cisco 0 03-19-2005 08:19 AM
ABC vs. CRTP? Mike Smith C++ 7 03-03-2005 05:43 PM



Advertisments