|
Teuchos - Trilinos Tools Package
Version of the Day
|
Default traits class for all conversions between value types. More...
#include <Teuchos_as.hpp>
Static Public Member Functions | |
| static TypeTo | convert (const TypeFrom t) |
| Convert t from a TypeFrom object to a TypeTo object. More... | |
| static TypeTo | safeConvert (const TypeFrom t) |
| Convert t from a TypeFrom object to a TypeTo object, with checks for validity. More... | |
Default traits class for all conversions between value types.
| TypeTo | The type to which to convert; the output type. |
| TypeFrom | The type from which to convert; the input type. |
The type conversion functions as() and asSafe() defined in this file use this traits class to convert between types. The default implementation of this class simply does an implicit type conversion. Syntactically, it expects either that TypeTo have a constructor which takes a single TypeFrom argument, like this:
or that TypeFrom has an "operator TypeTo()" method, like this:
Any conversions which are built into C++ and are safe to do will not need a traits class specialization, and should not generate any compiler warnings. This includes the conversions float to double, short to int, int to long, or an enum value to int.
Any conversion which is not syntactically legal according to the above rules must have a specialization. There are a number of examples in the header file below, including conversions between qd_real and various built-in types for which qd_real does not provide a native conversion operator. Other examples include std::string to int or double.
Any conversion that is syntactically legal, but could cause compiler warnings and/or result in incorrect behavior at run time should be given a specialization that does not rely on an implicit conversion. This includes the following:
type to and from unsigned type, where type is a built-in integer typedouble to int, or from any floating-point type to any integer type where overflow is possibleIf the user (through as() or asSafe()) requests a conversion for which no specialization of this class exists, then the default implementation below will be instantiated. If the conversion is not syntactically correct, then the compiler will report a compiler error. If the conversion is syntactically correct but unsafe, the compiler may report a warning. In either case, you can fix the error or warning by specializing this class for your combination of types. There are a number of examples of specializations in this header file, some of which include bounds checking for overflow (for safeConvert()).
The IEEE 754 standard defines the result of conversions from a larger to a smaller built-in floating-point type, including double to float, long double to float, and to long doubledouble. Such conversions might overflow (result in a value too large in magnitude to fit in the target type) or underflow (result in a value too small to fit in a normalized value of the target type). We never check for overflow or underflow for these conversions. Their behavior depends on the current rounding mode and whether your hardware and compiler correctly implement denormalized values. Typically, overflow results in an Inf of the same sign as the input, and underflow results in either a denormalized value or zero. If you want to do bounds checking, you should set the appropriate trap so that overflow or underflow will raise the SIGFPE signal. Please refer to the IEEE 754 standard for details. Note that safeConvert() conversions from e.g., std::string to built-in floating-point types still should check for overflow.
Definition at line 179 of file Teuchos_as.hpp.
|
inlinestatic |
Convert t from a TypeFrom object to a TypeTo object.
Definition at line 182 of file Teuchos_as.hpp.
|
inlinestatic |
Convert t from a TypeFrom object to a TypeTo object, with checks for validity.
Definition at line 189 of file Teuchos_as.hpp.
1.8.5