Update to a previous post
22 June, 2009 § 1 Comment
I’ve updated the post titled, “C++ Trick: Removing the need for Forward Declarations“. Further reading of The C++ Programming Language by Bjarne Stroustrup has pointed me to a partial explanation [1]:
For reasons that reach into the pre-history of C, it is possible to declare a struct and a non-structure with the same name in the same scope. For example:
struct stat { /* ... */ }; int stat( char* name, struct stat* buf);In that case, the plain name (stat) is the name of the non-structure, and the structure must be referred to with the prefix struct. Similarly, the keywords class, union (§C.8.2), and enum (§4.8) can be used as prefixes for disambiguation. However, it is best not to overload names to make that necessary.
That explains part of it. My assumption stands that because you are declaring that this name refers to a typename, then you will not need to forward declare it. I haven’t tried it yet, but I assume you can interchange `class` with `typename` just as well to declare that this is a type you are referring to.
[1] B. Stroustrup. The C++ Programming Language: 3rd Edition. Page 104.
I think the thing about having struct stat and the function stat is because in C, introducing a struct does not add it to the type system.
Generally what you see is something along these lines:
struct statStruct { /* */ };
typedef struct statStruct statType;
or
typedef struct { /* */ } stat;
It’s not until you do the typedef that it enters the type system.