to!string(double) at compile time

Bobby Bingham uhmmmm at gmail.com
Mon Aug 20 21:43:09 PDT 2012


I'm just getting started with D, and was playing around with string
mixins.  I've hit a snag, and have reduced it to a minimal test case:

    import std.conv;

    string test()
    {
        return to!string(0.0);
    }

    immutable auto testvar = mixin(test());

This gives this result when compiling:

/usr/include/phobos2/std/format.d(1479): Error: snprintf cannot be interpreted at compile time, because it has no available source code
/usr/include/phobos2/std/conv.d(99):        called from here: formatValue(w,src,f)
/usr/include/phobos2/std/conv.d(824):        called from here: toStr(value)
/usr/include/phobos2/std/conv.d(268):        called from here: toImpl(_param_0)
test.d(6):        called from here: to(0)
test.d(9):        called from here: test()
test.d(9): Error: argument to mixin must be a string, not (test())

I guess converting a double to a string can't be done at compile time
because it requires calling the C snprintf function?  It compiles fine
if I replace the 0.0 with an int literal.  Is there any way around
this limitation?


More information about the Digitalmars-d-learn mailing list