www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use core.vararg to print D variadic arguments and their types

reply BoQsc <vaidas.boqsc gmail.com> writes:
https://dlang.org/phobos/core_vararg.html

The common way to use **va_arg** is `va_arg!(int)(_argptr);`
What would be the alternative way or syntax that behave exactly 
the same way, even if more verbose?



____
`va_arg!(int)(_argptr);` is taken from an example in:
https://dlang.org/spec/function.html#d_style_variadic_functions
Sep 14 2023
parent reply Basile B. <b2.temp gmx.com> writes:
On Thursday, 14 September 2023 at 15:19:29 UTC, BoQsc wrote:
 https://dlang.org/phobos/core_vararg.html

 The common way to use **va_arg** is `va_arg!(int)(_argptr);`
 What would be the alternative way or syntax that behave exactly 
 the same way, even if more verbose?



 ____
 `va_arg!(int)(_argptr);` is taken from an example in:
 https://dlang.org/spec/function.html#d_style_variadic_functions
here's how ```d import core.vararg; void main() { foo(.5, 5); } void foo(...) { int i = void; va_arg(_argptr, typeid(i), &i); assert(i == 5); double d = void; va_arg(_argptr, typeid(d), &d); assert(d == .5); } ```
Sep 15 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/15/23 4:14 PM, Basile B. wrote:
 On Thursday, 14 September 2023 at 15:19:29 UTC, BoQsc wrote:
 https://dlang.org/phobos/core_vararg.html

 The common way to use **va_arg** is `va_arg!(int)(_argptr);`
 What would be the alternative way or syntax that behave exactly the 
 same way, even if more verbose?



 ____
 `va_arg!(int)(_argptr);` is taken from an example in:
 https://dlang.org/spec/function.html#d_style_variadic_functions
here's how ```d import core.vararg; void main() {     foo(.5, 5); } void foo(...) {     int i = void;     va_arg(_argptr, typeid(i), &i);     assert(i == 5);     double d = void;     va_arg(_argptr, typeid(d), &d);     assert(d == .5); } ```
Note that this doesn't work in gdc. The templated version is actually more akin to what C does. -Steve
Sep 16 2023
parent BoQsc <vaidas.boqsc gmail.com> writes:
 Note that this doesn't work in gdc.

 The templated version is actually more akin to what C does.
Yeah it does not seem to work in gdc when tested using https://d.godbolt.org/ The errors produced: ``` <source>:11:12: error: none of the overloads of template 'core.stdc.stdarg.va_arg' are callable using argument types '!()(__va_list_tag[1], TypeInfo, int*)' 11 | va_arg(_argptr, typeid(i), &i); | ^ /opt/compiler-explorer/gcc-trunk-20230917/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/ tdc/stdarg.d:179:7: note: Candidates are: 'va_arg(T)(ref va_list ap)' 179 | T va_arg(T)(ref va_list ap); // intrinsic | ^ /opt/compiler-explorer/gcc-trunk-20230917/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/s dc/stdarg.d:291:10: note: 'va_arg(T)(ref va_list ap, ref T parmn)' 291 | void va_arg(T)(ref va_list ap, ref T parmn); // intrinsic | ^ <source>:14:12: error: none of the overloads of template 'core.stdc.stdarg.va_arg' are callable using argument types '!()(__va_list_tag[1], TypeInfo, double*)' 14 | va_arg(_argptr, typeid(d), &d); | ^ /opt/compiler-explorer/gcc-trunk-20230917/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/ tdc/stdarg.d:179:7: note: Candidates are: 'va_arg(T)(ref va_list ap)' 179 | T va_arg(T)(ref va_list ap); // intrinsic | ^ /opt/compiler-explorer/gcc-trunk-20230917/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/s dc/stdarg.d:291:10: note: 'va_arg(T)(ref va_list ap, ref T parmn)' 291 | void va_arg(T)(ref va_list ap, ref T parmn); // intrinsic | ^ ASM generation compiler returned: 1 <source>:11:12: error: none of the overloads of template 'core.stdc.stdarg.va_arg' are callable using argument types '!()(__va_list_tag[1], TypeInfo, int*)' 11 | va_arg(_argptr, typeid(i), &i); | ^ /opt/compiler-explorer/gcc-trunk-20230918/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/ tdc/stdarg.d:179:7: note: Candidates are: 'va_arg(T)(ref va_list ap)' 179 | T va_arg(T)(ref va_list ap); // intrinsic | ^ /opt/compiler-explorer/gcc-trunk-20230918/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/s dc/stdarg.d:291:10: note: 'va_arg(T)(ref va_list ap, ref T parmn)' 291 | void va_arg(T)(ref va_list ap, ref T parmn); // intrinsic | ^ <source>:14:12: error: none of the overloads of template 'core.stdc.stdarg.va_arg' are callable using argument types '!()(__va_list_tag[1], TypeInfo, double*)' 14 | va_arg(_argptr, typeid(d), &d); | ^ /opt/compiler-explorer/gcc-trunk-20230918/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/ tdc/stdarg.d:179:7: note: Candidates are: 'va_arg(T)(ref va_list ap)' 179 | T va_arg(T)(ref va_list ap); // intrinsic | ^ /opt/compiler-explorer/gcc-trunk-20230918/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/s dc/stdarg.d:291:10: note: 'va_arg(T)(ref va_list ap, ref T parmn)' 291 | void va_arg(T)(ref va_list ap, ref T parmn); // intrinsic | ^ Execution build compiler returned: 1 ```
Sep 18 2023