digitalmars.D - Compile-time conversions from string to integer, real, etc.
- Don Clugston (17/17) Apr 13 2007 In std.metastring there are functions to convert integers to strings at
- Walter Bright (2/4) Apr 13 2007 Can these be replaced with .stringof ?
- Don Clugston (3/8) Apr 13 2007 Yup. It should probably be wrapped in a deprecated{} now. As should
- Don Clugston (11/19) Apr 13 2007 For example:
- Walter Bright (2/14) Apr 13 2007 I think AST macros should take care of this problem.
In std.metastring there are functions to convert integers to strings at compile time, but none to go the other way. Floating point conversions are the worst; there are so many different formats, worrying about round-off error, etc. It's a total nightmare. Luckily, this works... <g> /// Converts 'str' into a constant of type T. /// str may contain any D expression which evaluates to a literal. T evaluateLiteral(T, char [] str)() { mixin("return " ~ str ~ ";"\n); } // Examples: static assert(evaluateLiteral!(real, "1.234e-56") == 1.234e-56); static assert(evaluateLiteral!(cdouble, "(8.45+56i)*36.2i") == (8.45+56i)*36.2i); static assert(evaluateLiteral!(char [], `"abc"[1..2] ~ "def"`) == "bdef");
Apr 13 2007
Don Clugston wrote:In std.metastring there are functions to convert integers to strings at compile time,Can these be replaced with .stringof ?
Apr 13 2007
Walter Bright wrote:Don Clugston wrote:Yup. It should probably be wrapped in a deprecated{} now. As should std.math2.In std.metastring there are functions to convert integers to strings at compile time,Can these be replaced with .stringof ?
Apr 13 2007
Don Clugston wrote:Walter Bright wrote:For example: template toString(real x) { const char [] toString=x.stringof; } Unfortunately, none of these work inside a CTFE function. The problem is, that a CTFE function has to also work at runtime, and so it can't use compile-time variables in template value arguments (and it can't index tuples, etc). These kinds of operations are the only ones where template metaprogramming still survives.Don Clugston wrote:Yup. It should probably be wrapped in a deprecated{} now. As should std.math2.In std.metastring there are functions to convert integers to strings at compile time,Can these be replaced with .stringof ?
Apr 13 2007
Don Clugston wrote:For example: template toString(real x) { const char [] toString=x.stringof; } Unfortunately, none of these work inside a CTFE function. The problem is, that a CTFE function has to also work at runtime, and so it can't use compile-time variables in template value arguments (and it can't index tuples, etc). These kinds of operations are the only ones where template metaprogramming still survives.I think AST macros should take care of this problem.
Apr 13 2007