How do i forward declare a class that has been typedef'd?
-
22-07-2019 - |
Question
I have a string class that, unsurprisingly, uses a different implementation depending on whether or not UNICODE is enabled.
#ifdef UNICODE
typedef StringUTF16 StringT;
#else
typedef StringUTF8 StringT;
#endif
This works nicely but I currently have a problem where I need to forward declare the StringT typedef. How can I do this?
I can't do typedef StringT;
so it makes forward declaration tricky. Is it possible to do a forward declare of this typedef'd type without having to past the code above into the top of the header file?
Solution
Follow the example set by the iosfwd standard header. Write a header file that contains this, and call it StringTFwd.h
class StringUTF16;
class StringUTF8;
#ifdef UNICODE
typedef StringUTF16 StringT;
#else
typedef StringUTF8 StringT;
#endif
At least this is reusable and doesn't ugly up the headers that refer to it.
OTHER TIPS
You can't. (Rational: you can use typedef to define an alias for a basic type and those may use different ABI conventions depending on the precise type).
Can't you forward declare both StringUTF16 and StringUTF8 and then use your #idef
?