Home / Cplusplus / Type Conversion In C++

Type Conversion In C++

Some types are automatically converted to other types as needed.
 
For example, an integer value can always be used where a double value is expected. In the following:
 
a := 5
b :=a *.2
 
b is assigned the double value 1.0 and a’s type remains integer.
 
Similarly, a double value can be converted to a dcomplex value. Automatic conversions are limited to converting between numeric types, and converting a reference type to the type it refers to.
 
Other types require explicit conversion. For example, the following expression is illegal:
 
5 * “1234foo”
 
but the string can be explicitly converted to an integer using the function as_integer. The following yields an integer value of 6170:
 
5 * as_integer(“1234foo”)
 
The following functions are available for explicit type conversion:
 
as_boolean
as_byte
as_short
as_integer
as_float
as_double
as_complex
as_dcomplex
as_string
 
There are types that cannot be converted at all.
 
For example, a function type cannot be converted to any other type.
 
Type mismatches result in run-time errors.
 
Type Conversion
 
The following functions convert their argument to the stated type:
 
as_boolean(x)
as_byte(x)
as_short(x)
as_integer(x)
as_float(x)
as_double(x)
as_complex(x)
as_dcomplex(x)
as_string(x)
 
The argument x must be either numeric-or string-valued.
 
See § 3.1.3, page , for a discussion of implicit type conversion (i.e., not requiring the use of one of these functions).
 
Boolean Conversions
 
Conversion of a numeric values to Boolean yield T if the converted value is non-zero. A string value yields T if its length is non-zero.
 
For example:
 
as_ boolean([3.14159, 0])
 
yields [T, F], and as_boolean(“how are you?”)
 
 
yields [T, T, T], and as_boolean([”,’a’,”])
 
yields [F, T, F], and as_boolean(“.0000001”)
 
yields T, and as_boolean(“.0000001foo”)
 
and as_boolean(“0.”)
 
and as_boolean(0+9i)
 
yields T.
 
Note that an empty string here means a string with no text in it; this is different from a string with no elements.
 
as_boolean(”)
 
yields F, but as_boolean(“”)
 
yields [], an empty (boolean) vector.
 
Integer Conversions
 
A boolean value converted to byte, short, or integer yields 1 if the value was T and 0 if F. Conversions between byte, short, and integer types yields the same values as the host machine’s C++ compiler doing the same conversion via a cast.
 
A float or double value yields the same integer value as the host machine’s C++ compiler doing the same conversion via a cast. In particular, it is possible for a value like -3.14159 to be converted to -3 or -4 depending upon the particular compiler.
 
If the direction of this conversion is important, you can use floor and ceiling:
 
ceiling(x)
 
returns the smallest integer which is greater than or equal to the argument, x. This function can be abbreviated as ceil. If x is a vector, ceiling is applied to each of the elements of the vector.
 
floor(x)
 
returns the largest integer which is less than or equal to the argument, x. If x is a vector, floor is applied to each of the elements of the vector.
 
These will reliably convert floating point numbers to integers. complex or dcomplex values behave like float or double values except that complex or dcomplex values also lose their imaginary portion.
 
A string value is converted as per the C (and C++) routine atoi(). If the value is not a valid integer then it is converted to 0.
 
Float and Double Conversions
 
A boolean value converted to float or double yields 1.0 if T and 0.0 if F.
 
complex or dcomplex values lose their imaginary portion when converted to float or double.
 
A string value is converted as per the C (and C++) routine atof(). If the value is not a valid floating-point number then it is converted to 0.0.
 
Complex Conversions
 
A boolean value converted to complex or dcomplex yields 1.0+0.0i if T and 0.0+0.0i if F.
 
float or double numbers converted to complex or dcomplex results in a complex number whose real portion is equal to the float or double value, and whose imaginary portion is 0.0.
 
A string value is converted as per the C (and C++) routine atof().
 
String Conversions
 
boolean values when converted to a string yields “T” if true and “F” if false.
 
Byte, short, or integer values yield their natural string representation.
 
float values are converted as per printf()’s “%.6g” format.
 
double values are converted as per printf()’s “%.12g” format.
 
complex values are converted as per printf()’s “%.6g+%.6gi” format.
 
dcomplex values are converted as per printf()’s “%.12g+%.12gi” format. The conversion of floating point values to strings are changed by setting the system.print.precision value or the print.precision attribute for an individual value.