Skip to main content
GameDev.net gamedev.net
🔒 Locked

(Almost) recursive typedef

Started by rozz666 Jul 7, 2008 at 6:19 AM 4 replies 1.4k views
Original Post
rozz666
rozz666
I tried to make:

    typedef std::map<mcl::ptr<Island>, mcl::ptr<Node> > Node;
which of course didn't compile. (mcl::ptr is a smart pointer). So I found a way around it:

    class Node2;

    typedef std::map<mcl::ptr<Island>, mcl::ptr<Node2> > Node;
    typedef std::map<mcl::ptr<Island>, mcl::ptr<Node> > Node2;
Is it valid C++? It compiles under my compiler, but is it portable? PS. I know I can use a struct.
godecho
godecho
Quote:
Original post by rozz666
I tried to make:
    typedef std::map<mcl::ptr<Island>, mcl::ptr<Node> > Node;


which of course didn't compile. (mcl::ptr is a smart pointer).


The reason that doesn't compile is you're trying to use Node in it's own definition. You can't use the type 'Node' before it's defined.

Quote:
Original post by rozz666
So I found a way around it:
    class Node2;    typedef std::map<mcl::ptr<Island>, mcl::ptr<Node2> > Node;    typedef std::map<mcl::ptr<Island>, mcl::ptr<Node> > Node2;


Is it valid C++? It compiles under my compiler, but is it portable?

PS. I know I can use a struct.


Again, you're trying to use types (Node2 in the first typedef) before they've been defined. This doesn't work on any compilers I have on hand, and I can't see how it'd be standard C++, let alone portable.
ToohrVyk
ToohrVyk
Quote:
Original post by godecho
Again, you're trying to use types (Node2 in the first typedef) before they've been defined. This doesn't work on any compilers I have on hand, and I can't see how it'd be standard C++, let alone portable.


It's allowed, and portable, to use types before they are defined—this is precisely the point of forward declarations. The real question here is whether an instantiation of a standard library container (or any other class template) can be forward-declared as class, and I don't know the answer.
godecho
godecho
Quote:
Original post by ToohrVyk
Quote:
Original post by godecho
Again, you're trying to use types (Node2 in the first typedef) before they've been defined. This doesn't work on any compilers I have on hand, and I can't see how it'd be standard C++, let alone portable.


It's allowed, and portable, to use types before they are defined—this is precisely the point of forward declarations. The real question here is whether an instantiation of a standard library container (or any other class template) can be forward-declared as class, and I don't know the answer.


Allow me to amend my previous statement: You can't use a type before it's declared.

edit: I missed his forward declaration in his second example oops, :blush:
ToohrVyk
ToohrVyk
Quote:
Original post by godecho
Allow me to amend my previous statement: You can't use a type before it's declared. In his example, he didn't provide any forward declarations.


I'm fairly certain that class Node2; is a forward declaration in his second example.
dmatter
dmatter
Neither of them compile for me, nor could I get them working in that form since, AFAIK, it is not possible to forward declare a typedef.

If we assumed the latter was legal though then surely this would have worked for the first one:

class Node;
typedef std::map, mcl::ptr > Node;



The most-similar work around that I can think of is this:

class Node : public std::map, mcl::ptr > { };

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.