Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #168267

Re: complex problem with gcc usage

From Kaz Kylheku <864-117-4973@kylheku.com>
Newsgroups comp.lang.c
Subject Re: complex problem with gcc usage
Date 2022-11-18 20:03 +0000
Organization A noiseless patient Spider
Message-ID <20221118115253.118@kylheku.com> (permalink)
References <tl8eo4$2vpv0$1@dont-email.me>

Show all headers | View raw


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.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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