Interesting case of Incompatible pointer type warning

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I'm using GCC 7.2.0 and am trying to make a generic collection
(container of items) object in C.
1. Be verbose about the item type.
2. Compiler checks type when managing items.
3. Compiler checks pointer compatibility for both container type and
item type when passing collection pointer to another function.

///////////////////////////////////////
#include <stdio.h>

#define TYPE_WITH_SUBTYPE(type, subtype) \
	union { type; subtype* subtypePtr; }
#define SUBTYPE_OF(obj) \
	typeof(obj->subtypePtr[0])

typedef struct Collection { int a; int b; } Collection;
typedef struct MyItemType { double d; } MyItemType;

#define CollectionT(itemType) \
	TYPE_WITH_SUBTYPE(Collection, itemType)
#define CollectionT_Get(coll) \
	({ (SUBTYPE_OF(coll))NULL; })

void Fun2(CollectionT(MyItemType*)* coll)
{
	// correctly checks item type
	MyItemType* item = CollectionT_Get(coll);
}

void Fun1(CollectionT(MyItemType*)* coll)
{
	// can't get this to work (incompatible pointer warning)
	Fun2(coll);
}

int main(int argc, char* argv[])
{
	Fun1(NULL);
}
///////////////////////////////////////

So close.
The code works except for passing the collection pointer around, where
compiler reports incompatible pointers. The two anonymous unions are
considered different types.

Declaring / naming these unions would defeat the purpose in the given
case. Casting to void* is not an option because then we have no
typecheck (see pt.3).

Can we somehow make anonymous structures with *exactly* the same
definition be considered compatible?
Any other options I'm missing?

With kind regards,
BM



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux