views:

158

answers:

2

I know I can do

class Foo;

and probably

struct Bar;

and global functions

bool IsValid(int iVal);

What about a typed enum? What about a typed enum within an undeclared class? What about a function with an undeclared class? What about a static member within an undeclared class? What about these within an unknown namespace? Am I missing anything else that can be forward declared?

+6  A: 

You can forward declare

  • Templates, including partial specializations
  • Explicit specializations
  • Nested classes (this includes structs, "real" classes and unions)
  • Non-nested and local classes
  • Variables ("extern int a;")
  • Functions

If by "forward declaration" you strictly mean "declare but not define" you can also forward declare member functions. But you cannot redeclare them in their class definition once they are declared. You cannot forward-declare enumerations. I'm not sure whether I missed something.

Please note that all forward declarations listed above, except partial and explicit specializations, need to be declared using an unqualified name and that member functions and nested classes can only be declared-but-not-defined in their class definition.

class A { };
class A::B; // not legal

namespace A { }
void A::f(); // not legal

namespace A { void f(); } // legal

class B { class C; }; // legal
class B::C; // declaration-only not legal

class D { template<typename T> class E; };
template<typename T> class D::E<T*>; // legal (c.f. 14.5.4/6)
Johannes Schaub - litb
I think your forgot the new `enum class` from C++0x. The point of precising the underlying representation was to make it forward-declarable if I recall correctly.
Matthieu M.
A: 

extern int globalVar; where globalVar is declared in a separate compilation unit.

dgnorton