digitalmars.D.learn - vfprintf equivalent in D
- Sarath Kumar (5/5) Feb 23 2012 Hi,
- Jonathan M Davis (18/21) Feb 23 2012 If you're dealing with C variadics, then your going to need to use C
- Jonathan M Davis (10/43) Feb 23 2012 And if your intention was to specifically pass on arguments to writefln,...
- H. S. Teoh (11/14) Feb 23 2012 [...]
- Sarath Kumar (5/5) Feb 23 2012 Thanks Jonathan and Teoh. I just have to pass the var args in my functio...
Hi, Is there an equivalent to vfprintf(const char *fmt, , va_list ap) in D? -- Thanks, Sarath
Feb 23 2012
On Friday, February 24, 2012 05:22:58 Sarath Kumar wrote:Hi, Is there an equivalent to vfprintf(const char *fmt, , va_list ap) in D?If you're dealing with C variadics, then your going to need to use C functions. D doesn't just recreate C functions (at least, the standard library doesn't). C functions get wrapped when doing so adds some benefit, but with something like this, there's no reason to. If you're using typesafe variadics void func(int[] arr...) then you can just pass the array to the next function. If you're using D-style variadics (which are similar to C variadics but include type information), maybe there is, I don't know. Probably. I never use them. However, the typical thing to do in D is to use variadic templates. That's what writefln does. void func(Args...)(Args args) And with that, you can just pass the arguments on to another function. http://dlang.org/function.html http://dlang.org/template.html - Jonathan M Davis
Feb 23 2012
On Thursday, February 23, 2012 21:38:27 Jonathan M Davis wrote:On Friday, February 24, 2012 05:22:58 Sarath Kumar wrote:And if your intention was to specifically pass on arguments to writefln, then you can just do it, since it's a variadic template. void myWritefln(Args...)(Args args) { writefln(args); } But if you're looking to specifically pass on a C variadic, then use C's vfprintf. - Jonathan M DavisHi, Is there an equivalent to vfprintf(const char *fmt, , va_list ap) in D?If you're dealing with C variadics, then your going to need to use C functions. D doesn't just recreate C functions (at least, the standard library doesn't). C functions get wrapped when doing so adds some benefit, but with something like this, there's no reason to. If you're using typesafe variadics void func(int[] arr...) then you can just pass the array to the next function. If you're using D-style variadics (which are similar to C variadics but include type information), maybe there is, I don't know. Probably. I never use them. However, the typical thing to do in D is to use variadic templates. That's what writefln does. void func(Args...)(Args args) And with that, you can just pass the arguments on to another function. http://dlang.org/function.html http://dlang.org/template.html
Feb 23 2012
On Fri, Feb 24, 2012 at 05:22:58AM +0000, Sarath Kumar wrote:Hi, Is there an equivalent to vfprintf(const char *fmt, , va_list ap) in D?[...] You don't need one. You can just write: void myWritelnWrapper(T...)(T args) { ... // insert your own processing here, modify args // if you like, etc. writeln(args); } T -- Life would be easier if I had the source code. -- YHL
Feb 23 2012
Thanks Jonathan and Teoh. I just have to pass the var args in my function to writefln and Variadic templates is the right solution for me. -- Thanks, Sarath
Feb 23 2012