View source code
Display the source code in std/conv.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

Module std.conv

A one-stop shop for converting values from one type to another.

Category Functions
Generic asOriginalType castFrom emplace parse to toChars
Strings text wtext dtext hexString
Numeric octal roundTo signed unsigned
Exceptions ConvException ConvOverflowException

Functions

Name Description
asOriginalType Returns the representation of an enumerated value, i.e. the value converted to the base type of the enumeration.
dtext Convenience functions for converting one or more arguments of any type into text (the three character widths).
emplace Given a pointer chunk to uninitialized memory (but already typed as T), constructs an object of non-class type T at that address. If T is a class, initializes the class reference to null.
emplace Given a pointer chunk to uninitialized memory (but already typed as a non-class type T), constructs an object of type T at that address from arguments args. If T is a class, initializes the class reference to args[0].
emplace Given a raw memory area chunk (but already typed as a class type T), constructs an object of class type T at that address. The constructor is passed the arguments Args.
emplace Given a raw memory area chunk, constructs an object of class type T at that address. The constructor is passed the arguments Args.
emplace Given a raw memory area chunk, constructs an object of non-class type T at that address. The constructor is passed the arguments args, if any.
parse The parse family of functions works quite like the to family, except that:
  1. It only works with character ranges as input.
  2. It takes the input by reference. (This means that rvalues - such as string literals - are not accepted: use to instead.)
  3. It advances the input to the position following the conversion.
  4. It does not throw if it could not convert the entire input.
parse Parses a character input range to an integral value.
parse Takes a string representing an enum type and returns that type.
parse Parses a character range to a floating point number.
parse Parsing one character off a range returns the first element and calls popFront.
parse Parsing a character range to typeof(null) returns null if the range spells "null". This function is case insensitive.
parse Parses an array from a string given the left bracket (default '['), right bracket (default ']'), and element separator (by default ','). A trailing separator is allowed.
parse Parses an associative array from a string given the left bracket (default '['), right bracket (default ']'), key-value separator (default ':'), and element seprator (by default ',').
signed Returns the corresponding signed value for x (e.g. if x has type uint, it returns cast(int) x). The advantage compared to the cast is that you do not need to rewrite the cast if x later changes type (e.g from uint to ulong).
text Convenience functions for converting one or more arguments of any type into text (the three character widths).
toChars Convert integer to a range of characters. Intended to be lightweight and fast.
unsigned Returns the corresponding unsigned value for x (e.g. if x has type int, it returns cast(uint) x). The advantage compared to the cast is that you do not need to rewrite the cast if x later changes type (e.g from int to long).
wtext Convenience functions for converting one or more arguments of any type into text (the three character widths).

Classes

Name Description
ConvException Thrown on conversion errors.
ConvOverflowException Thrown on conversion overflow errors.

Templates

Name Description
castFrom A wrapper on top of the built-in cast operator that allows one to restrict casting of the original type of the value.
octal The octal facility provides a means to declare a number in base 8. Using octal!177 or octal!"177" for 127 represented in octal (same as 0177 in C).
roundTo Rounded conversion from floating point to integral.
to The to template converts a value from one type to another. The source type is deduced and the target type must be specified, for example the expression to!int(42.0) converts the number 42 from double to int. The conversion is "safe", i.e., it checks for overflow; to!int(4.2e10) would throw the ConvOverflowException exception. Overflow checks are only inserted when necessary, e.g., to!double(42) does not do any checking because any int fits in a double.

Enum values

Name Type Description
octal The octal facility provides a means to declare a number in base 8. Using octal!177 or octal!"177" for 127 represented in octal (same as 0177 in C).

Global variables

Name Type Description
hexString void Converts a hex literal to a string at compile time.

Authors

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

License

Boost License 1.0.

Comments