template struct polymorphic : public T ???



 DEVELOP > c-Plus-Plus > template struct polymorphic : public T ???

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "verec"
Date: 25 Jun 2005 10:55:32 AM
Object: template struct polymorphic : public T ???
Last week I asked here how I could detect that a T was polymorphic, and
received
very thoughtful and useful replies that I used straight away. Thanks to all who
answered.
This week, it turns out that detecting whether T is polymorphic clashes with
a new requirement, that T be allowed to not be complete.
Last week, this code used to compile:
typedef std::queue<Request> RequestQueue ;
typedef envelope<RequestQueue> Queue ;
This week, I now have to wrap std::queue into a polymorphic
container, as in:
struct RequestQueue : public std::queue<Request> {
// envelope does NOT work with monomorphic types
virtual ~RequestQueue() {} ;
} ;
typedef envelope<RequestQueue> Queue ;
Obviously, I'd like to create some kind of adaptor template ...
template <typename T> struct polymorphic : public T {
virtual ~polymorhic() {}
} ;
intended usage:
typedef polymorphic<std::queue<int> > Queue ;
But this doesn't compile!
The FAQ#35 seems mute here, and I'm beginning to suspect that
something is wrong with my syntax. I also checked
http://www.boost.org/libs/utility/call_traits.htm &
http://www.boost.org/libs/conversion/cast.htm (because it
contained the word: polymorphic_cast, but this turned out
a red-herring)
What would be the correct way to do this?
Many thanks, again.
--
JFB
.

User: "verec"

Title: Re: template struct polymorphic : public T ??? 25 Jun 2005 07:00:51 PM
On 2005-06-25 16:55:32 +0100, verec <verec@mac.com> said:

struct RequestQueue : public std::queue<Request> {
// envelope does NOT work with monomorphic types
virtual ~RequestQueue() {} ;
} ;

typedef envelope<RequestQueue> Queue ;

Obviously, I'd like to create some kind of adaptor template ...

template <typename T> struct polymorphic : public T {
virtual ~polymorhic() {}
} ;

I'm not sure exactly what went wrong, but this first version
was correct ...
#include <iostream>
#include <string>
#include <queue>
template <typename T> struct polymorphic : public T {
virtual ~polymorphic() {}
} ;
void
test_00001() {
typedef polymorphic<std::queue<int> > IntQueue ;
IntQueue queue ;
queue.push(5) ;
queue.push(6) ;
queue.push(7) ;
queue.push(8) ;
while(!queue.empty()) {
int i = queue.front() ;
queue.pop() ;
std::cout << "Just popped: " << i << std::endl ;
}
}
Though I couldn't find a single example of use that particular
syntax (which kind of turns templates inside out) it is
accepted by gcc 4.
--
JFB
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER