(Almost) recursive typedef

Started by
4 comments, last by dmatter 15 years, 9 months ago
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.
Advertisement
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.
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.
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:
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.
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> > { };

This topic is closed to new replies.

Advertisement