Sign in to follow this  
rozz666

(Almost) recursive typedef

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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<Island>, mcl::ptr<Node> > Node;



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

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this