When converting a float to a string from within a mixin an error is given.
Example:
import std.conv;
enum doubleString = mixin("to!string(0.6)");
Gives the following error:
..src\phobos\std\format.d(1355): Error: _snprintf cannot be interpreted at compile time, because it has no available source code
The reason for the error is clear: _snprintf is an external function and can therefore not be interpreted.
However, I think that compile-time conversion of floats to strings should be supported. Perhaps the formatting code can be ported to the std library instead of using an external function?
Comment #1 by hsteoh — 2014-09-21T23:54:38Z
The use of snprintf also causes other issues like purity/safety issues. I think we should roll our own native D implementation of these conversions so that it's usable in pure/safe/@nogc/ctfe.
Comment #2 by ag0aep6g — 2016-01-02T09:42:28Z
*** Issue 15497 has been marked as a duplicate of this issue. ***
Comment #3 by b2.temp — 2017-09-13T18:19:53Z
*** Issue 12181 has been marked as a duplicate of this issue. ***
Comment #5 by witold.baryluk+d — 2020-05-10T23:58:08Z
Any chance to make this come into phobos work with good tests? The D implementation could also be faster possible that what it is trying to do now (with snprintf, which does a lot of other stuff, including varags stuff, and can't be optimized well by D compiler).
I had this issue:
bench.d(358): called from here: to(bm(1LU, 18446744073709551615LU, null, null, 0.1F))
bench.d(358): while evaluating pragma(msg, "Generating benchmark code for: bench.bm_pow_Dop_14 using default or user options: " ~ to(bm(1LU, 18446744073709551615LU, null, null, 0.1F)))
bm is a struct here that has a float and double fields, and I want to use to!string on it during compile time for some compile time logging and error messages when some conditions are wrong.
Comment #6 by witold.baryluk+d — 2020-05-10T23:59:35Z
BTW. Mixing is not needed to trigger the issue:
import std.conv : to;
enum x = to!string(0.1);
is enough:
/usr/lib/ldc/x86_64-linux-gnu/include/d/std/exception.d(516): Error: uncaught CTFE exception std.format.FormatException("Cannot format floating point types at compile-time")
bench.d(338): called from here: to(0.1)
@berni44 created dlang/phobos pull request #7951 "std.format: Replace snprintf for x87-reals" fixing this issue:
- Fix Issue 8424 - Compile time conversions of double/floats to strings
https://github.com/dlang/phobos/pull/7951
Comment #12 by dlang-bot — 2021-04-12T07:25:24Z
dlang/phobos pull request #7951 "std.format: Replace snprintf for x87-reals" was merged into master:
- c75bd8756d148cf3de0b44fd71d4f667da44ef6c by berni44:
Fix Issue 8424 - Compile time conversions of double/floats to strings
https://github.com/dlang/phobos/pull/7951