to - multiple declarations

Function to

Performs checked conversion from an integral type From to an integral type To.

To to(To, From) (
  From from
)
if (isIntegral!From && isIntegral!To && !is(Unqual!To == Unqual!From) && !is(To == enum));

Parameters

NameDescription
From Source type.
To Target type.
from Source value.

Returns

from converted to To.

Throws

ConvException if from is too small or too large to be represented by To.

Function to

Converts a floating point number to an integral type.

To to(To, From) (
  From from
)
if (isFloatingPoint!From && isIntegral!To && !is(Unqual!To == Unqual!From) && !is(To == enum));

Parameters

NameDescription
From Source type.
To Target type.
from Source value.

Returns

Truncated from (everything after the decimal point is dropped).

Throws

ConvException if from < To.min || from > To.max.

Example

assert(1.5.to!int == 1);
assert(2147483646.5.to!int == 2147483646);
assert((-2147483647.5).to!int == -2147483647);
assert(2147483646.5.to!uint == 2147483646);

Function to

Performs checked conversion from an integral type From to an enum.

To to(To, From) (
  From from
)
if (isIntegral!From && is(To == enum));

Parameters

NameDescription
From Source type.
To Target type.
from Source value.

Returns

enum value.

Throws

ConvException if from is not a member of To.

Example

enum Test : int
{
    one,
    two,
}
static assert(is(typeof(1.to!Test) == Test));
assert(0.to!Test == Test.one);
assert(1.to!Test == Test.two);

Function to

Converts from to a boolean.

To to(To, From) (
  From from
)
if (isNumeric!From && is(Unqual!To == bool) && !is(Unqual!To == Unqual!From));

To to(To, From) (
  auto const ref From from
)
if ((is(From == String) || isSomeString!From) && is(Unqual!To == bool));

If From is a numeric type, then 1 becomes true, 0 false. Otherwise ConvException is thrown.

If To is a string (built-in string or String), then "true" or "false" are converted to the appropriate boolean value. Otherwise ConvException is thrown.

Parameters

NameDescription
From Source type.
To Target type.
from Source value.

Returns

from converted to a boolean.

Throws

ConvException if from isn't convertible.

Example

assert(!0.0.to!bool);
assert(0.2.to!bool);
assert(0.5.to!bool);
assert(1.0.to!bool);

assert(!0.to!bool);
assert(1.to!bool);

Example

assert("true".to!bool);
assert(!"false".to!bool);
assert(String("true").to!bool);
assert(!String("false").to!bool);

Function to

Converts a boolean to To.

To to(To, From) (
  From from
)
if (is(Unqual!From == bool) && isNumeric!To && !is(Unqual!To == Unqual!From));

If To is a numeric type, then true becomes 1, false 0.

If To is a String, then "true" or "false" is returned.

Parameters

NameDescription
From Source type.
To Target type.
from Source value.

Returns

from converted to To.

Example

assert(true.to!float == 1.0);
assert(true.to!double == 1.0);
assert(true.to!ubyte == 1);
assert(true.to!byte == 1);
assert(true.to!ushort == 1);
assert(true.to!short == 1);
assert(true.to!uint == 1);
assert(true.to!int == 1);

assert(false.to!float == 0);
assert(false.to!double == 0);
assert(false.to!ubyte == 0);
assert(false.to!byte == 0);
assert(false.to!ushort == 0);
assert(false.to!short == 0);
assert(false.to!uint == 0);
assert(false.to!int == 0);

Function to

Converts a stringish range to an integral value.

To to(To, From) (
  auto ref From from
)
if (isInputRange!From && isSomeChar!(ElementType!From) && isIntegral!To);

Parameters

NameDescription
From Source type.
To Target type.
from Source value.

Returns

from converted to To.

Throws

ConvException if from doesn't contain an integral value.

Example

assert("1234".to!uint() == 1234);
assert("1234".to!int() == 1234);
assert("1234".to!int() == 1234);

assert("0".to!int() == 0);
assert("-0".to!int() == 0);

assert("0x10".to!int() == 16);
assert("0X10".to!int() == 16);
assert("-0x10".to!int() == -16);

assert("0b10".to!int() == 2);
assert("0B10".to!int() == 2);
assert("-0b10".to!int() == -2);

assert("010".to!int() == 8);
assert("-010".to!int() == -8);

assert("-128".to!byte == cast(byte) -128);

Template to

If the source type From and the target type To are equal, does nothing. If From can be implicitly converted to To, just returns from.

template to(To) ;

Contained Functions

NameDescription
to

Parameters

NameDescription
To Target type.

Returns

from.

Example

auto val = 5.to!int();
assert(val == 5);
static assert(is(typeof(val) == int));