Here's a puzzle: I'd like to define something like typedef std::pair<int,P*> P; I can obviously do class P : public std::pair<int,P*>{}; or typedef std::pair<int,void*> P; The first gives me objects of class P where P.second is a pointer to a subclass of what I really want. The second gives me something where, with careful use of reinterpret_cast, I can create pointers to what I really want. The puzzle is can I create a standard pair where pair.second is a pointer to an element of the class I've created without using reinterpret_cast in some way? There is a real point to this: I'd like to avoid reinterpret_cast so that g++ can detect errors better in a complicated template class (a protected subclass of std::multimap that can act like a linked list) but am constrained to use std::pair and not some convenient derived class. -- JDL
On Sunday 21 August 2005 3:51 pm, John D Lamb wrote:
Here's a puzzle: I'd like to define something like
typedef std::pair<int,P*> P;
I can obviously do
class P : public std::pair<int,P*>{};
or
typedef std::pair<int,void*> P; In this case, you have not yet defined P. You could use a forward declaration, such as: class P; Then, typedef std::pair<int,P*> P; should work. I have not tried your example specifically. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
Jerry Feldman wrote:
On Sunday 21 August 2005 3:51 pm, John D Lamb wrote:
Here's a puzzle: I'd like to define something like
typedef std::pair<int,P*> P;
I can obviously do
class P : public std::pair<int,P*>{};
or
typedef std::pair<int,void*> P; In this case, you have not yet defined P. You could use a forward declaration, such as: class P; Then, typedef std::pair<int,P*> P; should work. I have not tried your example specifically.
Sadly it doesn't work though I actually had to try this to see. It does confirm that I'm not being totally stupid in thinking that C++ might reasonably allow this sort of thing. The C++ standard doesn't allow you to use typedef for a name already declared in the same scope. I think C++ won't allow you to do something like: class X<X*> x; i.e. create a template class whose template parameter is a pointer to an object of this class. It will allow class Y : public class X<Y*> {} x; which is nearly the same. For my own problem, I've worked out I can use static_cast rather than reinterpret_cast, which is a bit of an improvement. -- JDL
Hello Jerry, I tried this and It worked for me on g++ (3.4.4). #include<list> using namespace std; std::list<class P*> P; /*I tried for list template, it should work for pair also*/ typedef class P* Y; typdef std::list<Y> P_new; /*P_new is the class what u are expecting.*/ HTH, Vamsi kundeti On 8/24/05, John D Lamb <J.D.Lamb@btinternet.com> wrote:
Jerry Feldman wrote:
On Sunday 21 August 2005 3:51 pm, John D Lamb wrote:
Here's a puzzle: I'd like to define something like
typedef std::pair<int,P*> P;
I can obviously do
class P : public std::pair<int,P*>{};
or
typedef std::pair<int,void*> P; In this case, you have not yet defined P. You could use a forward declaration, such as: class P; Then, typedef std::pair<int,P*> P; should work. I have not tried your example specifically.
Sadly it doesn't work though I actually had to try this to see. It does confirm that I'm not being totally stupid in thinking that C++ might reasonably allow this sort of thing.
The C++ standard doesn't allow you to use typedef for a name already declared in the same scope.
I think C++ won't allow you to do something like:
class X<X*> x;
i.e. create a template class whose template parameter is a pointer to an object of this class. It will allow
class Y : public class X<Y*> {} x;
which is nearly the same.
For my own problem, I've worked out I can use static_cast rather than reinterpret_cast, which is a bit of an improvement.
-- JDL
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
vamsi krishna wrote:
I tried this and It worked for me on g++ (3.4.4).
#include<list> using namespace std;
std::list<class P*> P; /*I tried for list template, it should work for pair also*/ typedef class P* Y; typdef std::list<Y> P_new;
/*P_new is the class what u are expecting.*/
Unfortunately it doesn't quite work though it took me a while to work out why: I hadn't thought of declaring a class inside the brackets <...>. There are two classes P here I think: one declared but not defined and the other defined as std::list<struct P*>. The problem is that neither has type std::list<std::list<...>*> that I need. The code compiles correctly because the declarations are correct. You can construct a list of type std::list<std::list<struct P*>*> but you can't do anything with the objects it contains because their class has never been defined. Still it proves there are things I hadn't thought of trying.. -- JDL
participants (3)
-
Jerry Feldman
-
John D Lamb
-
vamsi krishna