porton
porton

Reputation: 5801

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

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

Upvotes: 2

Views: 54

Answers (1)

dhasenan
dhasenan

Reputation: 1228

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.

Upvotes: 3

Related Questions