The rational is that I'd like to create a function `new_thing()` which
takes an optional size, e.g.,
#define NARGS(...) NARGS_(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
This does *not* work for the `va_test()` call. But if I supply 1 or 2 args
it works great.
Subject: Re: is it possible to have functions with 0, 1, or 2 args?
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) { ... }
```
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
#define NARGS(...) NARGS_(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
Up top, C's totally buggin' about the diff between one argument and
zilch, 'cause that comma in "__VA_ARGS__," is always chillin'
there. We could 86 it with the fresh "__VA_OPT__(,)", but not all C
implementations are down to clown with that yet . . .
Mark Summerfield <mark@qtrac.eu> wrote or quoted:
This does *not* work for the `va_test()` call. But if I supply 1 or 2 args >> it works great.
Here's an
SSCCE, a short self-contained correct compilable example, aka,
MWE - minimal working example.
Some coders whip up these examples themselves for their post!
It's a quick and dirty demo that can be compiled and run to
show what's buggin' them.
In this case, it's hella clear the macros are trippin', thinkin'
the number of args is "1" when it's actually zilch, nada, zero.
main.c
#include <stdio.h>
#define NARGS(...) NARGS_(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
#define NARGS_(_5, _4, _3, _2, _1, N, ...) N
#define va_test(...) NARGS(__VA_ARGS__)
int main( void ){ printf( "%d\n", va_test() ); }
transcript
1
On Mon, 05 Aug 2024 08:26:04 +0000
Mark Summerfield <mark@qtrac.eu> wrote:
According to my understanding, vararg functions without arguments will
become possible in C23. But since fully functional C23 compilers do not exist yet, right now the answer to your question is "No".
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) { ... }
```
Michael S <already5chosen@yahoo.com> writes:
On Mon, 05 Aug 2024 08:26:04 +0000
Mark Summerfield <mark@qtrac.eu> wrote:
According to my understanding, vararg functions without arguments will
become possible in C23. But since fully functional C23 compilers do not
exist yet, right now the answer to your question is "No".
Even in C99 varargs functions can be called without arguments.
The question here is more subtle. In fact it is possible even
in C99 and C11 to do what he wants, but it isn't easy, and it
is far from obvious how to go about it. Still, if the question
is "can this be done?", the answer is Yes even now.
On 12/08/2024 06:20, Tim Rentsch wrote:
Michael S <already5chosen@yahoo.com> writes:
On Mon, 05 Aug 2024 08:26:04 +0000
Mark Summerfield <mark@qtrac.eu> wrote:
According to my understanding, vararg functions without arguments will
become possible in C23. But since fully functional C23 compilers do not >>> exist yet, right now the answer to your question is "No".
Even in C99 varargs functions can be called without arguments.
The question here is more subtle. In fact it is possible even
in C99 and C11 to do what he wants, but it isn't easy, and it
is far from obvious how to go about it. Still, if the question
is "can this be done?", the answer is Yes even now.
How does va_start know where to, um, start from if there's no last
named argument?
Richard Harnden <richard.nospam@gmail.invalid> writes:
On 12/08/2024 06:20, Tim Rentsch wrote:
Michael S <already5chosen@yahoo.com> writes:
On Mon, 05 Aug 2024 08:26:04 +0000
Mark Summerfield <mark@qtrac.eu> wrote:
According to my understanding, vararg functions without arguments will >>>> become possible in C23. But since fully functional C23 compilers do not >>>> exist yet, right now the answer to your question is "No".
Even in C99 varargs functions can be called without arguments.
The question here is more subtle. In fact it is possible even
in C99 and C11 to do what he wants, but it isn't easy, and it
is far from obvious how to go about it. Still, if the question
is "can this be done?", the answer is Yes even now.
How does va_start know where to, um, start from if there's no last
named argument?
Sorry, I meant to say macros, not functions (using ... and __VA_ARGS__).
The original question is about macro definitions, not function
definitions, and I didn't read carefully enough.
On 12/08/2024 10:28, Tim Rentsch wrote:
Richard Harnden <richard.nospam@gmail.invalid> writes:
On 12/08/2024 06:20, Tim Rentsch wrote:
Michael S <already5chosen@yahoo.com> writes:
On Mon, 05 Aug 2024 08:26:04 +0000
Mark Summerfield <mark@qtrac.eu> wrote:
According to my understanding, vararg functions without arguments will >>>>> become possible in C23. But since fully functional C23 compilers do not >>>>> exist yet, right now the answer to your question is "No".
Even in C99 varargs functions can be called without arguments.
The question here is more subtle. In fact it is possible even
in C99 and C11 to do what he wants, but it isn't easy, and it
is far from obvious how to go about it. Still, if the question
is "can this be done?", the answer is Yes even now.
How does va_start know where to, um, start from if there's no last
named argument?
Sorry, I meant to say macros, not functions (using ... and __VA_ARGS__).
The original question is about macro definitions, not function
definitions, and I didn't read carefully enough.
No worries.
So ... how can you do it with macros?
And, even if it's possible, is it a good idea?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 01:42:23 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,579 |