# (Almost) recursive typedef

This topic is 3753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Quote:
 Original post by rozz666I tried to make: typedef std::map, mcl::ptr > 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 rozz666So I found a way around it: class Node2; typedef std::map, mcl::ptr > Node; typedef std::map, mcl::ptr > 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 on other sites
Quote:
 Original post by godechoAgain, 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 on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by godechoAgain, 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 on other sites
Quote:
 Original post by godechoAllow 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 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> > { };

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009199
• ### Who's Online (See full list)

There are no registered users currently online

×