Sorry, the main function is not a good example, better:
int _tmain(int argc, _TCHAR* argv[])
{
myStruct s;
s.x = 5;
return 0;
}
Main Topics
Browse All TopicsI am integrating a number of 3rd partly library databases (for benchmarking) and I would like to keep the specific implementation details of this out of the header files. To this end I am forward declaring everything; however, I've just discovered one of the libraries defines an anonymous struct and typedefs it, something like this...
typedef struct {} MyStruct;
I can't forward declare this because the compiler quite rightly complains it finds a typedef that was previous declared as a struct.
My question then is, is it actually possible to forward declare this typedef? I've never encountered a reason to try and do this before so I hate to admit it but I am stumped :)
Just a simple yes, here's how or no is all I am after and not a lengthy discussion on the pros and cons of forward declaration vs. including the headers in my header -- I promise I know all the arguments ;)
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi evilrix,
I don't think it's possible in a direct way. Unfortunateley I cannot proof it by posting a link to a spec or something, but I think at least one good reason against it is that a typedef'ed name can be a 'void' - since it's not possible to declare a reference to void this isn't possible.
How do you intend to use this? Do you need to use a 'MyStruct&' within your code's header in own functions? Then maybe a forwarded struct can help you, i.e.:
// in header
> struct _MyStruct;
> void foobar( _MyStruct& m );
// in cpp
> typedef struct {} MyStruct;
>
> struct _MyStruct : public MyStruct {};
>
> void foobar( _MyStruct& m )
> {
> ...
> }
@jaime_olivares: I don't think this first sample is of any help since the struct in question is an anonymous struct - otherwise it would be simple.
ZOPPO
>> Hm - couldn't you implement this with a template? I.e. as in the example below?
Not really cos then all the implementation would need to go in the header (to satisfy the requirements of the 2 part instantiation of a template) and the type would need to be known when I define the concrete instance, moving the implementation detail outside of the class :)
Like I said, the solution is just to include the 3rd party header in my header... but I just want to know if I am overlooking something about forward declaring anonymous structs. I'm not really after an alternative solution -- I have one.
>> probably, DBHANDLE is just an int. If it gives you an idea...
DBHANDLE was just an example I made up to show an example of what I am doing :)
In my specific case it is not an int it *IS* a struct -- I have the source code. But that is not in question. My question, my only question is, "Is it possible to forward declare a typedef'ed anonymous struct?".
This question is largely academic. I am not looking for solutions to a problem (the code is now written and works just fine) I just want my curiosity quashed :)
Anonymous types provide a convenient way to encapsulate a set of read-only properties
It's about C#, but logically it should be right in C too.
Another links:
but you know all that.
http://anubis.dkuug.dk/jtc
exte
Please take a look.
pgnatyuk,
Thanks but none of that really helps ;)
=-=-=-=-=-=-=-=-=-=-=-=-
BTW
=-=-=-=-=-=-=-=-=-=-=
The code below accurately models the problem.
g++ -g x.cpp -o xprog
x.cpp:13: error: conflicting declaration typedef struct foo foo
x.cpp:2: error: foo has a previous declaration as typedef struct foo foo
make: *** [xprog] Error 1
>> My question then is, is it actually possible to forward declare this typedef?
You cannot forward declare a typedef for an anonymous struct, for the simple reason that it's anonymous, and thus has no tag (so you can't refer to it, nor forward declare it).
It does however have a name in the typedef namespace, which you can use to refer to the anonymous struct (as an alias), but that still doesn't allow you to forward declare it (which is only possible with tags).
An ugly solution is to use a void* in the header, and cast it to the proper type in the .cpp file, but its ugliness I'm sure invalidates it as a solution for you :)
The wrapper approach you already mentioned yourself is another way, although it has some overhead.
Another alternative is to not include the header file in the .cpp file. The error won't occur, because there are no conflicting types at compile time. You can then declare the struct in the header file as an incomplete type :
struct MyStruct;
or even copy the whole typedef in your header file :
typedef struct { /* all members here */ } MyStruct;
I haven't tried it, but I see no reason why that shouldn't work.
>> heh. I pretty much knew this was the case but really need to double check I wasn't being a pilchard :)
The standard doesn't allow it, because a forward declaration needs a tag (from the tag namespace) to unambiguously refer to the struct definition.
I know you knew it, but just wanted to confirm it :)
Business Accounts
Answer for Membership
by: jaime_olivaresPosted on 2009-10-28 at 05:41:53ID: 25682539
This works at least with Visual Studio:
#include "stdafx.h"
struct myStruct;
typedef struct myStruct myStruct;
struct myStruct
{
public:
int x;
};
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}