Sujet : Re: is it possible to have functions with 0, 1, or 2 args?
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 12. Aug 2024, 06:15:52
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <861q2ugw3b.fsf@linuxsc.com>
References : 1
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Mark Summerfield <
mark@qtrac.eu> writes:
Given
>
```
// vaargs.h
#pragma once
>
#define NARGS(...) NARGS_(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
#define NARGS_(_5, _4, _3, _2, _1, N, ...) N
>
#define CONC(A, B) CONC_(A, B)
#define CONC_(A, B) A##B
>
// va_test.h
#include "vaargs.h"
>
#define va_test(...) CONC(va_test, NARGS(__VA_ARGS__))(__VA_ARGS__)
int va_test0();
int va_test1(int);
int va_test2(int, int);
>
// va_test.c
>
#include "va_test.h"
>
int va_test0() { return va_test2(3, 11); }
int va_test1(int a) { return va_test2(3, a); }
int va_test2(int a, int b) { return a + b; }
>
// va_tests.c
#include "va_test.h"
#include <stdbool.h>
#include <stdio.h>
>
int main() {
int i = va_test();
if (i != 14) {
fprintf(stderr, "FAIL: va_test() expecte 14 go %d\n", i);
}
i = va_test(5);
if (i != 8) {
fprintf(stderr, "FAIL: va_test() expecte 8 go %d\n", i);
}
i = va_test(2, 9);
if (i != 11) {
fprintf(stderr, "FAIL: va_test() expecte 11 go %d\n", i);
}
}
```
>
This does *not* work for the `va_test()` call. But if I supply 1 or 2 args
it works great.
>
The rational is that I'd like to create a function `new_thing()` which
takes an optional size, e.g.,
>
```
thing new_thing0() { return new_thing1(10); }
// above uses default size of 10
thing new_thing1(int size) { ... }
```
The short answer is, it is possible, from C99 onwards, to define a
macro va_test(...) that does what you want. It isn't easy, but
it is possible.
If it's really important to define a macro that behaves like what
you describe, and you can't find a suitable workaround, feel free
to ask again and I can try to find and work through materials I
have somewhere, to give an explanation of how to do what you want.
Please note that I am making no guarantees about whether I can
accomplish that, only that I can try to do so.
Date | Sujet | # | | Auteur |
5 Aug 24 | is it possible to have functions with 0, 1, or 2 args? | 15 | | Mark Summerfield |
5 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 1 | | Stefan Ram |
5 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 4 | | Stefan Ram |
5 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 2 | | Stefan Ram |
8 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 1 | | Lawrence D'Oliveiro |
11 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 1 | | Tim Rentsch |
5 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 6 | | Michael S |
12 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 5 | | Tim Rentsch |
12 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 4 | | Richard Harnden |
12 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 3 | | Tim Rentsch |
12 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 2 | | Richard Harnden |
13 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 1 | | Tim Rentsch |
5 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 1 | | Stefan Ram |
5 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 1 | | Blue-Maned_Hawk |
12 Aug 24 | Re: is it possible to have functions with 0, 1, or 2 args? | 1 | | Tim Rentsch |