Can I forward declare a type defined elsewhere with typedef

Viewed 110

I know that I can forward declare a class or struct that is defined in another header file (that I do not want to include). I'm trying to do the same thing with a plain old data type which is specified by a typedef.

class foo; // defined in foo.h
struct bar; // defined in bar.h
typedef baz; // defined in baz.h

void Crunch(foo& a, bar& b, baz& c);

Obviously the above code does not compile (error: 'baz' does not name a type) and if the line is removed, compilation fails with error: 'baz' has not been declared. If I change the word typedef to using in the above example, the error is expected nested-name-specifier before 'baz'.

Is there some equivalent to forward declaring the type baz?

1 Answers

You can’t do this, because there is nothing at all that can be done with a type without knowing what kind of type it is. In particular, baz might be a function type, in which case const baz and baz are the same type (affecting overload resolution), or it could be an lvalue reference type, in which case those, baz&, and baz&& are all the same type. It could be volatile void or int () &&, in which case baz& doesn’t exist. The language simply doesn’t support this level of “utterly incomplete type”.

One could imagine a declaration that said that “This is some unspecified object type”, but some platforms use different addressing conventions for, say, char and int, so we can’t have that either if we expect to be able to use pointers or references to such an unknown type. (This doesn’t apply to class types, whose corresponding pointer types are specified to behave similarly.)

Related