Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #168275
| From | aotto1968 <aotto1968@t-online.de> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: complex problem with gcc usage |
| Date | 2022-11-19 09:43 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <tla4vb$36ma1$1@dont-email.me> (permalink) |
| References | <tl8eo4$2vpv0$1@dont-email.me> <20221118115253.118@kylheku.com> |
Hi,
the "_Generic feature looks good and I found a solution:
/// cast a \e known-object into an \RMkNs{ObjectS} reference
#define MkOBJ_R(x) (_Generic((x),struct MkObjectS *:(*x),default:((*(x)).super.obj)))
/// cast a \e known-object into an \RMkNs{ObjectS} pointer
#define MkOBJ(x) (&MkOBJ_R(x))
and now the next problem arrive, this will *NOT* compile with C++.
I compile my "C" also together with "C++" code into a C++ namespace with the C++ compiler…
(just using the C++ as an other "quality-check" and to NOT use an external "c" library etc…
but C++ say: Why can't I use _Generic in C++ code?
→ stackoverflow.com/questions/42496875/why-cant-i-use-generic-in-c-code
in C++ they mention to use "function-Overload" but this is a MACRO and the core problem is
→ the "default" in "_Generic" also work for FUTURE code but an Overload have to be defined
at compile-time.
On 18.11.22 21:03, Kaz Kylheku wrote:
> On 2022-11-18, aotto1968 <aotto1968@t-online.de> wrote:
>> Hi,
>>
>> My problem is I want to write macro/function able to use different code depending on the pointer argument.
>>
>> → MkOBJ(pointer)
>>
>> the pointer is from a "struct" and have to change the behavior if the field "pointer->super.obj" is available or not.
>> I like to use something like:
>>
>> #define MkOBJ(ptr) ifexists(ptr->super.obj) ? ptr->super.obj : ptr
>>
>> 1) thee are structs with "super.obj" filed and pointers without "super.obj" field.
>> 2) I want to use a SINGLE macro "MkOBJ" to work on both kind of pointers.
>> 3) I know there is "typesize" "offsetof" "typeof" but where is the "ifexists" ?
>> 4) I want to have a compile-time solution and no run-time "work-arount"
>
> The _Generic mechanism ("generic selection") in C11 might be able to do
> this. It's a relatively recent C feature which provides essentially a
> compile-time type switch/case to select different variants of code
> (which have to be expressions) based on the type of an expression.
>
> _Generic will not give you a solution whereby the pointer can be to any
> structure type whatsoever which has a super.obj member; you have to
> commit to specific types that are all named in the selection.
>
> E.g. suppose you have "struct foo" and "struct bar" which have this
> super.obj. I think it goes something like:
>
> _Generic(pointer, // <-- expression used for type only
> struct foo * : pointer->super.obj,
> struct bar * : pointer->super.obj,
> default : pointer)
>
> If pointer is a struct foo * or struct bar *, then use the
> expression pointer->super.obj. Otherwise the pointer expression.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
complex problem with gcc usage aotto1968 <aotto1968@t-online.de> - 2022-11-18 18:17 +0100
Re: complex problem with gcc usage Kaz Kylheku <864-117-4973@kylheku.com> - 2022-11-18 20:03 +0000
Re: complex problem with gcc usage aotto1968 <aotto1968@t-online.de> - 2022-11-19 09:43 +0100
Re: complex problem with gcc usage Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-11-19 14:39 +0000
Re: complex problem with gcc usage Kaz Kylheku <864-117-4973@kylheku.com> - 2022-11-19 16:13 +0000
Re: complex problem with gcc usage aotto1968 <aotto1968@t-online.de> - 2022-11-19 19:19 +0100
Re: complex problem with gcc usage Kaz Kylheku <864-117-4973@kylheku.com> - 2022-11-19 21:42 +0000
Re: complex problem with gcc usage Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-11-19 14:34 -0800
Re: complex problem with gcc usage aotto1968 <aotto1968@t-online.de> - 2022-11-20 08:40 +0100
Re: complex problem with gcc usage Kaz Kylheku <864-117-4973@kylheku.com> - 2022-11-20 15:09 +0000
Re: complex problem with gcc usage "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-11-18 12:10 -0800
csiph-web