View source code Display the source code in std/conv.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Template std.conv.castFrom

A wrapper on top of the built-in cast operator that allows one to restrict casting of the original type of the value.

A common issue with using a raw cast is that it may silently continue to compile even if the value's type has changed during refactoring, which breaks the initial assumption about the cast.

Arguments

template castFrom(From);

Functions

Function name Description
to

Parameters

NameDescription
From The type to cast from. The programmer must ensure it is legal to make this cast.

Example

// test all the hex digits
static assert( ("0123456789abcdefABCDEF").isHexLiteral);
// empty or white strings are not valid
static assert( "\r\n\t".isHexLiteral);
// but are accepted if the count of hex digits is even
static assert( "A\r\n\tB".isHexLiteral);

Authors

Walter Bright, Andrei Alexandrescu, Shin Fujishiro, Adam D. Ruppe, Kenji Hara

License

Boost License 1.0.

Comments