public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112101] New: feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments
@ 2023-10-26 14:33 malekwryyy at gmail dot com
  2023-10-26 14:43 ` [Bug c/112101] " malekwryyy at gmail dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: malekwryyy at gmail dot com @ 2023-10-26 14:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112101

            Bug ID: 112101
           Summary: feature request: typeof_arg for extracting the type of
                    a function's (or function pointer's) arguments
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: malekwryyy at gmail dot com
  Target Milestone: ---

C23 will add typeof (although gcc had a for a while) which gives the type of an
expression or a type. By using it, it is possible to get the return type of a
function like so:

```
int func();

typeof(func()) x; // int x; 
```

But there's no way to extract the type of the argument of a function:

```
void func(int);

?? x;
```
I think something like 'typeof_arg' would be a good addition. 

It takes 2 operands, first is function or function pointer, and second is an
integer constant for the index of the argument, which must be within [0,
arg_count).

for example:
```
#define print_func(f) \
printf(#f \
"(" \
_Generic( (__typeof_arg(f, 0)){0}, \
    int:     "int", \
    long:    "long", \
    float:   "float", \
    char*:   "char*", \
    default: "other ") \
")")
```
this would print a single-argument function's name and arg type like this
"puts(char*)".

another example:
```
#define gurantee_type(exp, type) \
_Generic(exp, type: exp, default: (typeof(exp)){0})

#define call_with_empty(f) \
_Generic( (__typeof_arg(f, 0)){0}, \
char*: gurantee_type(f, void(*)(char*))(""), \
default: f( (__typeof_arg(f, 0)){0} ) \
)
```
which calls the function 'f' with empty string if it takes char*, or 0 of the
correct type otherwise.

this wouldn't work for variadic functions, so __typeof_arg(printf, 1) would be
an error.

I think a feature like this would be really helpful for generic programming in
C

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/112101] feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments
  2023-10-26 14:33 [Bug c/112101] New: feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments malekwryyy at gmail dot com
@ 2023-10-26 14:43 ` malekwryyy at gmail dot com
  2023-10-26 20:09 ` pinskia at gcc dot gnu.org
  2023-10-27 11:23 ` malekwryyy at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: malekwryyy at gmail dot com @ 2023-10-26 14:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112101

--- Comment #1 from Abdulmalek Almkainzi <malekwryyy at gmail dot com> ---
correction for the gurantee_type macro:

#define gurantee_type(exp, type) \
_Generic(exp, type: exp, default: (type){0})

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/112101] feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments
  2023-10-26 14:33 [Bug c/112101] New: feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments malekwryyy at gmail dot com
  2023-10-26 14:43 ` [Bug c/112101] " malekwryyy at gmail dot com
@ 2023-10-26 20:09 ` pinskia at gcc dot gnu.org
  2023-10-27 11:23 ` malekwryyy at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-26 20:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112101

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c/112101] feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments
  2023-10-26 14:33 [Bug c/112101] New: feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments malekwryyy at gmail dot com
  2023-10-26 14:43 ` [Bug c/112101] " malekwryyy at gmail dot com
  2023-10-26 20:09 ` pinskia at gcc dot gnu.org
@ 2023-10-27 11:23 ` malekwryyy at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: malekwryyy at gmail dot com @ 2023-10-27 11:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112101

--- Comment #2 from Abdulmalek Almkainzi <malekwryyy at gmail dot com> ---
Another correction, I'm sorry, its a bit hard to write hypothetical code.
the macro print_func:

#define print_func(f) \
printf( \
_Generic( (__typeof_arg(f, 0)){0}, \
    int:     #f "(int)", \
    long:    #f "(long)", \
    float:   #f "(float)", \
    char*:   #f "(char*)", \
    default: #f "(other)") \
)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-10-27 11:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-26 14:33 [Bug c/112101] New: feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments malekwryyy at gmail dot com
2023-10-26 14:43 ` [Bug c/112101] " malekwryyy at gmail dot com
2023-10-26 20:09 ` pinskia at gcc dot gnu.org
2023-10-27 11:23 ` malekwryyy at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).