Difference between cast(T)x and to!T(x)

Viewed 48

What is the difference between cast(T)x and to!T(x) in D programming language? (provided that the module std.conv is imported)

1 Answers

cast(T)x does a few types of conversions as guaranteed by the language. These include:

  • dynamic casts converting between class / interface types, provided by the runtime
  • numeric type casts, eg double to short
  • array type casts, eg int[] to ubyte[]
  • forcing an alias this conversion that would otherwise not be forced
  • type system-only conversions, especially around const and shared

to!T(x) does a number of other conversions, most notably to and from strings.

Related