Variadic template arguments unpacking

Max Strakhov monnoroch at gmail.com
Sat Jul 6 05:10:50 PDT 2013


On Saturday, 6 July 2013 at 01:35:28 UTC, Ali Çehreli wrote:
> On 07/05/2013 05:41 PM, Max Strakhov wrote:
> Here is one that works:
>
> import core.stdc.stdio;
> import std.string;
>
> string convFormat(string s)
> {
>     return s ~ '\n';
> }
>
> void myPrintf(T...)(string s, T args)
> {
>     printf(convFormat(s).toStringz, args);
> }
>
> void main()
> {
>     myPrintf("format %d %f %c %s", 42, 1.5, 'a', 
> "hello".toStringz);
> }

You only converted the format string with was not in the 
question. I need to convert all args by a template function, but 
your example is actually better than mine.
Suppose, i want my own string representation of primitive types, 
then in c++ i would write those two functions:
convFormat("format %d %f %c %s") -> std::string("format %s %s %s 
%s")
conv(42) -> std::string("int(42)")
conv(1.5)-> std::string("float(1.5)")
etc.
and the resulting printf:
template<typename ... Args>
void myPrintf(const char * format, Args&& ... args) {
    printf(convFormat(format).c_str(), 
conv(std::forward<Args>(args)).c_str()...);
}
And that would do the job. Can i somehow do the same thing in D?


More information about the Digitalmars-d-learn mailing list