Teuchos - Trilinos Tools Package Version of the Day
Static Public Member Functions
Teuchos::ValueTypeConversionTraits< TypeTo, TypeFrom > Class Template Reference

Default traits class for all conversions between value types. More...

#include <Teuchos_as.hpp>

List of all members.

Static Public Member Functions

static TypeTo convert (const TypeFrom t)
 Convert t from a TypeFrom object to a TypeTo object.
static TypeTo safeConvert (const TypeFrom t)
 Convert t from a TypeFrom object to a TypeTo object, in a more safe way.

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() 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 type to type, type to long type, or an enum value to int (where type may be either int or unsigned 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 qd_real. Other examples include std::string to int, double, etc.

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

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 126 of file Teuchos_as.hpp.


Member Function Documentation

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

Convert t from a TypeFrom object to a TypeTo object.

Definition at line 129 of file Teuchos_as.hpp.

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

Convert t from a TypeFrom object to a TypeTo object, in a more safe way.

Definition at line 136 of file Teuchos_as.hpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines