Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.c > #387500
| Path | csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: is it possible to have functions with 0, 1, or 2 args? |
| Date | Sun, 11 Aug 2024 22:15:52 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 73 |
| Message-ID | <861q2ugw3b.fsf@linuxsc.com> (permalink) |
| References | <7q-dnbTDU4oBES37nZ2dnZfqnPednZ2d@brightview.co.uk> |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Date | Mon, 12 Aug 2024 07:15:56 +0200 (CEST) |
| Injection-Info | dont-email.me; posting-host="f42e4005105099d89c60a754521770ce"; logging-data="3317302"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xFPwXZeQzCgyXUiwK+ptEReNoVcSfMeY=" |
| User-Agent | Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) |
| Cancel-Lock | sha1:I/Zm6HPRAwz1Q8zqS34Tvj1mWnE= sha1:iZSKR1Qh8cvPryjHLvXqcy+xFeg= |
| Xref | csiph.com comp.lang.c:387500 |
Show key headers only | View raw
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.
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar
is it possible to have functions with 0, 1, or 2 args? Mark Summerfield <mark@qtrac.eu> - 2024-08-05 08:26 +0000
Re: is it possible to have functions with 0, 1, or 2 args? Michael S <already5chosen@yahoo.com> - 2024-08-05 15:19 +0300
Re: is it possible to have functions with 0, 1, or 2 args? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 22:20 -0700
Re: is it possible to have functions with 0, 1, or 2 args? Richard Harnden <richard.nospam@gmail.invalid> - 2024-08-12 09:33 +0100
Re: is it possible to have functions with 0, 1, or 2 args? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 02:28 -0700
Re: is it possible to have functions with 0, 1, or 2 args? Richard Harnden <richard.nospam@gmail.invalid> - 2024-08-12 11:37 +0100
Re: is it possible to have functions with 0, 1, or 2 args? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-12 15:39 -0700
Re: is it possible to have functions with 0, 1, or 2 args? Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-08-05 18:46 +0000
Re: is it possible to have functions with 0, 1, or 2 args? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-07 23:04 +0000
Re: is it possible to have functions with 0, 1, or 2 args? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 10:43 -0700
Re: is it possible to have functions with 0, 1, or 2 args? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 22:15 -0700
csiph-web