Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Static Public Member Functions | List of all members
Teuchos::ValueTypeConversionTraits< TypeTo, TypeFrom > Class Template Reference

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...
 

Detailed Description

template<class TypeTo, class TypeFrom>
class Teuchos::ValueTypeConversionTraits< TypeTo, TypeFrom >

Default traits class for all conversions between value types.

Note
Users should never call this class directly. Please use the as() or asSafe() template functions.
Template Parameters
TypeToThe type to which to convert; the output type.
TypeFromThe 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:

class TypeTo {
public:
TypeTo (const TypeFrom& x);
}

or that TypeFrom has an "operator TypeTo()" method, like this:

class TypeFrom {
public:
operator TypeTo ();
}

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:

If 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 long double to double. 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.

Note
We cannot promise that converting from T1 to T2 and back again will result in the same T1 value with which we started. For example, converting from a long long to a double may result in truncation, since long long has 63 bits of significand and double has 53.

Definition at line 147 of file Teuchos_as.hpp.

Member Function Documentation

template<class TypeTo , class TypeFrom >
static TypeTo Teuchos::ValueTypeConversionTraits< TypeTo, TypeFrom >::convert ( const TypeFrom  t)
inlinestatic

Convert t from a TypeFrom object to a TypeTo object.

Definition at line 150 of file Teuchos_as.hpp.

template<class TypeTo , class TypeFrom >
static TypeTo Teuchos::ValueTypeConversionTraits< TypeTo, TypeFrom >::safeConvert ( const TypeFrom  t)
inlinestatic

Convert t from a TypeFrom object to a TypeTo object, with checks for validity.

Definition at line 157 of file Teuchos_as.hpp.


The documentation for this class was generated from the following file: