Forward typedefs

J

Jorge Yáñez

Hello,

It's possible to do a forward declaration of a typedef?

Something similar to

typedef TMyType; // typedef forward
declaration

void f ( TMyType& t )
{
// do something with t
}

...

typedef double TMyType; // typedef declaration


The code above doesn't compile.

In Item 37 of "More Exceptional C++", Herb Sutter says that is not
possible to do a forward declaration of std::eek:stream ( that is a typedef )
because it's part of the std namespace. Does this means that typedef forward
declarations for non-std members are possible?

Thanks.
 
B

Buster

Jorge said:
It's possible to do a forward declaration of a typedef?

No. What are you trying to accomplish?
Something similar to

typedef TMyType; // typedef forward
declaration

void f ( TMyType& t )
{
// do something with t
}

...

typedef double TMyType; // typedef declaration


The code above doesn't compile.

It's incorrect. There's this:

template <typename T> void f (T & t)
{
// do something with t
}

double x;
f (x);
In Item 37 of "More Exceptional C++", Herb Sutter says that is not
possible to do a forward declaration of std::eek:stream ( that is a typedef )
because it's part of the std namespace. Does this means that typedef forward
declarations for non-std members are possible?

No.
 
D

David Harmon

On Fri, 17 Dec 2004 09:20:48 +0100 in comp.lang.c++, "Jorge Yáñez"
In Item 37 of "More Exceptional C++", Herb Sutter says that is not
possible to do a forward declaration of std::eek:stream ( that is a typedef )
because it's part of the std namespace.

You cannot forward declare std::eek:stream because it is a typedef of a
tricky instance of a templated class, and the class must be declared
before you can typedef it, and you do not know or want to know
enough to properly and exactly declare that class. If you just
tried to write
namespace std { class ostream; };
it would contradict the correct definition when it comes along
later.

The library author knows about those things, and can forward declare
std::eek:stream. Have a look at the contents of your compiler's
#include <iosfwd>
which is provided for that purpose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top