[C++-sig] problems with typedef void *

Niall Douglas s_sourceforge at nedprod.com
Tue Dec 14 23:28:28 CET 2010


On 14 Dec 2010 at 14:07, Jacob Davis wrote:

> I read through the thread back when I started work on this problem, but
> figured if it had been scrubbed out it might not be wise to use it. I'll go
> back and check it out soon, though.

Trampoline types are a very commonly used idiom in C++ 
metaprogramming. They let you work around pre-C++0x limitations.

> The last I saw on the subject, void
> pointers were supposed to "just work", but I wonder if they "just work"
> outside the context of a typedef obfuscation. If this doesn't work out, I'll
> probably just do an extra layer of wrapping as Jim suggested.

There is absolutely no reason why you can't declare all your 
typedefed void * as opaque pointers and get to work. The only problem 
will be that BPL won't know what to do with them (as they all look 
like void * and therefore are indistinguishable) and therefore can't 
touch them in any way except to pass them around unmodified. This 
probably won't be much use to you.

I might add that another - and better solution - is to modify the C 
library to use proper opaque structures like you're supposed to in C 
rather than void * for everything. I would assume that below the 
header API its source is casting them to internal structures anyway, 
so you should be able to modify its header to use declared but not 
defined structs with the names that it's typedefing them to e.g.

typedef void *EmoEngineEventHandle;

... this goes to:

struct EmoEngineEventHandle_t;
typedef struct EmoEngineEventHandle_t *EmoEngineEventHandle;

Theoretically this should let you compile your C library just fine if 
it is indeed casting to internal structs. Even if it doesn't, it 
would be very easy to fix.

It isn't 100% legal C or C++ though as it's type punning. However I 
can't think of a compiler which won't allow it.

Niall

-- 
Technology & Consulting Services - ned Productions Limited.
http://www.nedproductions.biz/. VAT reg: IE 9708311Q. Company no: 
472909.





More information about the Cplusplus-sig mailing list