Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_as.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_AS_HPP
43 #define TEUCHOS_AS_HPP
44 
70 
71 #include "Teuchos_ConfigDefs.hpp"
72 #include "Teuchos_Assert.hpp"
73 #include <limits>
74 #include <cstdlib>
75 #include <cerrno>
76 #include <climits>
77 
78 #ifdef HAVE_TEUCHOS_COMPLEX
79 #include <complex>
80 #endif // HAVE_TEUCHOS_COMPLEX
81 
82 #ifdef HAVE_TEUCHOS_QD
83 #include <qd/qd_real.h>
84 #include <qd/dd_real.h>
85 #endif // HAVE_TEUCHOS_QD
86 
87 namespace Teuchos {
88 
89 
178 template<class TypeTo, class TypeFrom>
180 public:
182  static TypeTo convert (const TypeFrom t) {
183  // This default implementation is just an implicit conversion and
184  // may generate compiler warnings on dangerous conversions.
185  return t;
186  }
187 
189  static TypeTo safeConvert (const TypeFrom t) {
190  // This default implementation is just an implicit conversion and
191  // may generate compiler warnings on dangerous conversions. No
192  // runtime checking (e.g., for overflow) can be done by default;
193  // only specializations can define meaningful and portable
194  // run-time checks of conversions.
195  return t;
196  }
197 };
198 
286 template<class TypeTo, class TypeFrom>
287 inline TypeTo as( const TypeFrom& t )
288 {
289 #ifdef HAVE_TEUCHOS_DEBUG
291 #else
293 #endif // HAVE_TEUCHOS_DEBUG
294 }
295 
296 
362 template<class TypeTo, class TypeFrom>
363 inline TypeTo asSafe( const TypeFrom& t )
364 {
366 }
367 
368 
381 template <class TypeTo>
382 class asFunc {
383 public:
384  asFunc() {}
385 
386  template <class TypeFrom>
387  inline TypeTo operator()(const TypeFrom &t) {
388  return as<TypeTo>(t);
389  }
390 };
391 
392 
393 
394 namespace { // anonymous
395 
413  template<class IntType>
414  IntType
415  intToString (const std::string& t,
416  IntType (*rawConvert) (const char*, char**, int),
417  const char* intTypeName)
418  {
419  // We call the "raw" conversion function instead of using
420  // std::istringstream, because we want more detailed information
421  // in case of failure to convert. I have no idea what
422  // operator>>(std::istream&, unsigned long long&) does if it
423  // encounters an integer too long to fit in IntType, for example.
424  //
425  // mfh 13 Nov 2012: It's fair to assume that if you have "long
426  // long", then your implementation of the C standard library
427  // includes strtoul(). Ditto for "unsigned long long" and
428  // strtoull(). If this is not the case, we could include a
429  // configure-time test for these functions(), with a fall-back to
430  // an std::istringstream operator>> implementation.
431  char* endptr = NULL;
432  // Keep the pointer, because std::string doesn't necessarily
433  // guarantee that this is the same across calls to c_str(), does
434  // it? Or perhaps it does...
435  const char* t_ptr = t.c_str ();
436  // We preset errno to 0, to distinguish success or failure after
437  // calling strtoull. Most implementations of the C standard
438  // library written with threads in mind have errno be a macro that
439  // expands to thread-local storage. Thanks to the Linux
440  // documentation for strtol ("man 3 strtol", Red Hat Enterprise
441  // Linux 5) for advice with the following checks.
442  errno = 0;
443  const IntType val = rawConvert (t_ptr, &endptr, 10);
444 
445  const IntType minVal = std::numeric_limits<IntType>::min ();
446  const IntType maxVal = std::numeric_limits<IntType>::max ();
448  errno == ERANGE && (val == minVal || val == maxVal),
449  std::range_error,
450  "Teuchos::ValueTypeConversionTraits<" << intTypeName << ", std::string>::convert: "
451  "The integer value in the given string \"" << t << "\" overflows " << intTypeName << ".");
453  errno != 0 && val == 0,
454  std::invalid_argument,
455  "Teuchos::ValueTypeConversionTraits<" << intTypeName << ", std::string>::convert: "
456  "The conversion function was unable to convert the given string \"" << t << "\" to " << intTypeName << ".");
458  endptr == t_ptr, // See above discussion of c_str().
459  std::invalid_argument,
460  "Teuchos::ValueTypeConversionTraits<" << intTypeName << ", std::string>::convert: "
461  "The conversion function was unable to read any integer digits from the given string "
462  "\"" << t << "\".");
463  return val;
464  }
465 
477  template<class OutputRealType, class InputRealType>
478  OutputRealType
479  realToReal (const InputRealType& x, const bool doBoundsChecking)
480  {
482 
483  if (doBoundsChecking) {
484  // For floating-point types T, std::numeric_limits<T>::min()
485  // returns the smallest positive value. IEEE 754 types have a
486  // sign bit, so the largest-magnitude negative value is the
487  // negative of the largest-magnitude positive value.
488  const OutputRealType minVal = -std::numeric_limits<OutputRealType>::max ();
489  const OutputRealType maxVal = std::numeric_limits<OutputRealType>::max ();
490 
491  // NaN is neither less than nor greater than anything. We just
492  // let it pass through, per the rules for propagation of silent
493  // NaN. (Signaling NaN will signal, but that's OK.)
495  x < minVal || x > maxVal,
496  std::range_error,
497  "realToReal<" << TypeNameTraits<OutputRealType>::name () << ", "
498  << TypeNameTraits<InputRealType>::name () << ">: "
499  "Input value x = " << x << " is out of the valid range [" << minVal
500  << ", " << maxVal << "] for conversion to the output type.");
501  }
502 
503  // Call as() and not static_cast, because there might not
504  // necessarily be a conversion defined between the two types,
505  // other than via as(). Definitely don't call asSafe(), because
506  // that could cause infinite recursion.
507  return as<OutputRealType> (x);
508  }
509 
510 
535  template<class RealType>
536  RealType
537  stringToReal (const std::string& t,
538  RealType (*rawConvert) (const char*, char**),
539  const char* realTypeName)
540  {
541  if (rawConvert == NULL) {
542  std::istringstream in (t);
543  RealType out;
544  in >> out;
545  return out;
546  }
547  else {
548  char* endptr = NULL;
549  // Keep the pointer, because std::string doesn't necessarily
550  // guarantee that this is the same across calls to c_str(), does
551  // it? Or perhaps it does...
552  const char* t_ptr = t.c_str ();
553  // We preset errno to 0, to distinguish success or failure after
554  // calling strtoull. Most implementations of the C standard
555  // library written with threads in mind have errno be a macro that
556  // expands to thread-local storage. Thanks to the Linux
557  // documentation for strtod ("man 3 strtod", Red Hat Enterprise
558  // Linux 5) for advice with the following checks.
559  errno = 0;
560  const RealType val = rawConvert (t_ptr, &endptr);
561 
563  errno == ERANGE && (val != 0),
564  std::range_error,
565  "Teuchos::ValueTypeConversionTraits<" << realTypeName
566  << ", std::string>::convert: "
567  "The value in the given string \"" << t << "\" overflows "
568  << realTypeName << ".");
569  //
570  // mfh 20 Nov 2012: Should we treat underflow as an error?
571  //
573  errno == ERANGE && val == 0,
574  std::invalid_argument,
575  "Teuchos::ValueTypeConversionTraits<" << realTypeName
576  << ", std::string>::convert: "
577  "The value in the given string \"" << t << "\" underflows "
578  << realTypeName << ".");
580  endptr == t_ptr, // See above discussion of c_str().
581  std::invalid_argument,
582  "Teuchos::ValueTypeConversionTraits<" << realTypeName
583  << ", std::string>::convert: "
584  "The conversion function was unable to read any floating-point data "
585  "from the given string \"" << t << "\".");
586  return val;
587  }
588  }
589 
590 } // namespace (anonymous)
591 
592 
593 //
594 // Standard specializations of ValueTypeConversionTraits
595 //
596 
597 //
598 // * Partial specialization for conversion from std::string to any type T.
599 // There are full specializations for specific types T below.
600 //
601 
614 template<class OutType>
615 class ValueTypeConversionTraits<OutType, std::string> {
616 public:
617  static OutType safeConvert (const std::string& t) {
618  return convert (t);
619  }
620 
621  static OutType convert (const std::string& t) {
622  std::istringstream in (t);
623  OutType out;
624  t >> out;
625  return out;
626  }
627 };
628 
629 //
630 // * Specializations for conversions from std::string to build-in
631 // real-valued floating-point types.
632 //
633 
635 template<>
636 class ValueTypeConversionTraits<double, std::string> {
637 public:
638  static double convert (const std::string& t) {
639  return stringToReal<double> (t, &strtod, "double");
640  }
641 
642  static double safeConvert (const std::string& t) {
643  return stringToReal<double> (t, &strtod, "double");
644  }
645 };
646 
648 template<>
649 class ValueTypeConversionTraits<float, std::string> {
650 public:
651  static float convert (const std::string& t) {
652 #ifdef _ISOC99_SOURCE
653  return stringToReal<float> (t, &strtof, "float");
654 #else
655  // strtof is new in C99. If you don't have it, just use strtod
656  // and convert the resulting double to float.
657  const double d = stringToReal<double> (t, &strtod, "double");
658  return realToReal<float, double> (d, false);
659 #endif // _ISOC99_SOURCE
660  }
661 
662  static float safeConvert (const std::string& t) {
663 #ifdef _ISOC99_SOURCE
664  return stringToReal<float> (t, &strtof, "float");
665 #else
666  // strtof is new in C99. If you don't have it, just use strtod
667  // and convert the resulting double to float.
668  const double d = stringToReal<double> (t, &strtod, "double");
669  return realToReal<float, double> (d, true);
670 #endif // _ISOC99_SOURCE
671  }
672 };
673 
675 template<>
676 class ValueTypeConversionTraits<long double, std::string> {
677 public:
678  static long double convert (const std::string& t) {
679 #ifdef _ISOC99_SOURCE
680  return stringToReal<long double> (t, &strtold, "long double");
681 #else
682  // strtof is new in C99. If you don't have it, just use
683  // operator>>(std::istream&, long double&).
684  return stringToReal<long double> (t, NULL, "long double");
685 #endif // _ISOC99_SOURCE
686  }
687 
688  static long double safeConvert (const std::string& t) {
689  return convert (t);
690  }
691 };
692 
693 
694 //
695 // * Specializations for conversions from std::string to build-in integer types.
696 //
697 
698 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
699 
703 template<>
704 class ValueTypeConversionTraits<long long, std::string> {
705 public:
711  static long long safeConvert (const std::string& t) {
712 #if defined(_MSC_VER)
713  // Windows does not implement strtoull, so we resort to a
714  // fallback. Thanks to Ross Bartlett for pointing out _strtoi64.
715  // See the MSDN reference [last accessed 21 Mar 2013]:
716  //
717  // http://msdn.microsoft.com/en-us/library/h80404d3%28v=vs.80%29.aspx
718  return intToString<long long> (t, &_strtoi64, "long long");
719 #else
720  return intToString<long long> (t, &strtoll, "long long");
721 #endif // defined(_MSC_VER)
722  }
723 
725  static long long convert (const std::string& t) {
726  return safeConvert (t);
727  }
728 };
729 
730 
734 template<>
735 class ValueTypeConversionTraits<unsigned long long, std::string> {
736 public:
742  static unsigned long long safeConvert (const std::string& t) {
743 #if defined(_MSC_VER)
744  // Windows does not implement strtoull, so we resort to a
745  // fallback. The fallback does not know how to check for under-
746  // or overflow. Alas, Windows does not seem to have an equivalent
747  // of _strtoi64 for unsigned long long.
748  const char intTypeName[] = "unsigned long long";
749  std::istringstream istr (t);
750  unsigned long long i = 0;
751  istr >> i;
753  ! istr, std::invalid_argument,
754  "Teuchos::ValueTypeConversionTraits<" << intTypeName << ", std::string>::"
755  "convert: Unable to convert the given string \"" << t << "\" to " <<
756  intTypeName << ". Windows lacks strtoull(), so we had to resort to a "
757  "fall-back conversion. The fall-back method does not know how to test "
758  "for overflow.");
759  return i;
760 #else
761  return intToString<unsigned long long> (t, &strtoull, "unsigned long long");
762 #endif // defined(_MSC_VER)
763  }
764 
766  static unsigned long long convert (const std::string& t) {
767  return safeConvert (t);
768  }
769 };
770 
771 #endif // HAVE_TEUCHOS_LONG_LONG_INT
772 
773 
777 template<>
778 class ValueTypeConversionTraits<long, std::string> {
779 public:
785  static long safeConvert (const std::string& t) {
786  return intToString<long> (t, &strtol, "long");
787  }
788 
790  static long convert (const std::string& t) {
791  return safeConvert (t);
792  }
793 };
794 
795 
799 template<>
800 class ValueTypeConversionTraits<unsigned long, std::string> {
801 public:
807  static unsigned long safeConvert (const std::string& t) {
808  return intToString<unsigned long> (t, &strtoul, "unsigned long");
809  }
810 
812  static unsigned long convert (const std::string& t) {
813  return safeConvert (t);
814  }
815 };
816 
817 
821 template<>
822 class ValueTypeConversionTraits<int, std::string> {
823 private:
825  static long safeConvertToLong (const std::string& t) {
826  long val = 0;
827  try {
829  } catch (std::range_error&) {
831  true,
832  std::range_error,
833  "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: "
834  "The given std::string \"" << t << "\" is too big to fit into long, so there is no way it could fit into int.");
835  } catch (std::invalid_argument& e) {
837  true,
838  std::invalid_argument,
839  "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: "
840  "Intermediate conversion from std::string to long failed, with the following error message: "
841  << e.what ());
842  }
843  return val;
844  }
845 
846 public:
852  static int safeConvert (const std::string& t) {
853  return asSafe<int> (safeConvertToLong (t));
854  }
855 
857  static int convert (const std::string& t) {
858  return as<int> (safeConvertToLong (t));
859  }
860 };
861 
862 
866 template<>
867 class ValueTypeConversionTraits<unsigned int, std::string> {
868 private:
870  static unsigned long safeConvertToUnsignedLong (const std::string& t) {
871  unsigned long val = 0;
872  try {
873  val = as<unsigned long> (t);
874  } catch (std::range_error&) {
876  true,
877  std::range_error,
878  "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: "
879  "The given std::string \"" << t << "\" is too big to fit into unsigned long, so there is no way it could fit into unsigned int.");
880  } catch (std::invalid_argument& e) {
882  true,
883  std::invalid_argument,
884  "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: "
885  "Intermediate conversion from std::string to unsigned long failed, with the following error message: "
886  << e.what ());
887  }
888  return val;
889  }
890 
891 public:
897  static unsigned int safeConvert (const std::string& t) {
898  return asSafe<unsigned int> (safeConvertToUnsignedLong (t));
899  }
900 
902  static unsigned int convert (const std::string& t) {
903  return as<unsigned int> (safeConvertToUnsignedLong (t));
904  }
905 };
906 
907 
911 template<>
912 class ValueTypeConversionTraits<short, std::string> {
913 private:
915  static long safeConvertToLong (const std::string& t) {
916  long val = 0;
917  try {
919  } catch (std::range_error&) {
921  true,
922  std::range_error,
923  "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: "
924  "The given std::string \"" << t << "\" is too big to fit into long, so there is no way it could fit into short.");
925  } catch (std::invalid_argument& e) {
927  true,
928  std::invalid_argument,
929  "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: "
930  "Intermediate conversion from std::string to long failed, with the following error message: "
931  << e.what ());
932  }
933  return val;
934  }
935 
936 public:
942  static short safeConvert (const std::string& t) {
943  return asSafe<short> (safeConvertToLong (t));
944  }
945 
947  static short convert (const std::string& t) {
948  return as<short> (safeConvertToLong (t));
949  }
950 };
951 
952 
956 template<>
957 class ValueTypeConversionTraits<unsigned short, std::string> {
958 private:
960  static unsigned long safeConvertToUnsignedLong (const std::string& t) {
961  unsigned long val = 0;
962  try {
963  val = as<unsigned long> (t);
964  } catch (std::range_error&) {
966  true,
967  std::range_error,
968  "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: "
969  "The given std::string \"" << t << "\" is too big to fit into unsigned long, so there is no way it could fit into unsigned short.");
970  } catch (std::invalid_argument& e) {
972  true,
973  std::invalid_argument,
974  "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: "
975  "Intermediate conversion from std::string to unsigned long failed, with the following error message: "
976  << e.what ());
977  }
978  return val;
979  }
980 
981 public:
987  static unsigned short safeConvert (const std::string& t) {
988  return asSafe<unsigned short> (safeConvertToUnsignedLong (t));
989  }
990 
992  static unsigned short convert (const std::string& t) {
993  return as<unsigned short> (safeConvertToUnsignedLong (t));
994  }
995 };
996 
997 //
998 // * Specializations for conversions between built-in real-valued
999 // floating-point types (like float and double).
1000 //
1001 
1003 template<>
1004 class ValueTypeConversionTraits<float, double> {
1005 public:
1006  static float safeConvert (const double t) {
1007  // mfh 25 Nov 2012: Disabling bounds checking, in favor of IEEE
1008  // 754 overflow semantics. Users who want bounds checking should
1009  // set the appropriate trap.
1010 #if 0
1011  // For floating-point types T, std::numeric_limits<T>::min()
1012  // returns the smallest positive value. IEEE 754 types have a
1013  // sign bit, so the largest-magnitude negative value is the
1014  // negative of the largest-magnitude positive value.
1015  const float minVal = -std::numeric_limits<float>::max ();
1016  const float maxVal = std::numeric_limits<float>::max ();
1017 
1018  // NaN is neither less than nor greater than anything. We just
1019  // let it pass through, per the rules for propagation of silent
1020  // NaN. (Signaling NaN will signal, but that's OK.)
1022  t < minVal || t > maxVal,
1023  std::range_error,
1024  "Teuchos::ValueTypeConversionTraits<float, double>::safeConvert: "
1025  "Input double t = " << t << " is out of the valid range [" << minVal
1026  << ", " << maxVal << "] for conversion to float.");
1027 #endif // 0
1028 
1029  return static_cast<float> (t);
1030  }
1031 
1032  static float convert (const double t) {
1033  return static_cast<float> (t);
1034  }
1035 };
1036 
1037 
1039 template<>
1040 class ValueTypeConversionTraits<float, long double> {
1041 public:
1042  static float safeConvert (const long double t) {
1043  // mfh 25 Nov 2012: Disabling bounds checking, in favor of IEEE
1044  // 754 overflow semantics. Users who want bounds checking should
1045  // set the appropriate trap.
1046 #if 0
1047  // For floating-point types T, std::numeric_limits<T>::min()
1048  // returns the smallest positive value. IEEE 754 types have a
1049  // sign bit, so the largest-magnitude negative value is the
1050  // negative of the largest-magnitude positive value.
1051  const float minVal = -std::numeric_limits<float>::max ();
1052  const float maxVal = std::numeric_limits<float>::max ();
1053 
1054  // NaN is neither less than nor greater than anything. We just
1055  // let it pass through, per the rules for propagation of silent
1056  // NaN. (Signaling NaN will signal, but that's OK.)
1058  t < minVal || t > maxVal,
1059  std::range_error,
1060  "Teuchos::ValueTypeConversionTraits<float, long double>::safeConvert: "
1061  "Input long double t = " << t << " is out of the valid range [" << minVal
1062  << ", " << maxVal << "] for conversion to float.");
1063 #endif // 0
1064 
1065  return static_cast<float> (t);
1066  }
1067 
1068  static float convert (const long double t) {
1069  return static_cast<float> (t);
1070  }
1071 };
1072 
1073 
1075 template<>
1076 class ValueTypeConversionTraits<double, long double> {
1077 public:
1078  static double safeConvert (const long double t) {
1079  // mfh 25 Nov 2012: Disabling bounds checking, in favor of IEEE
1080  // 754 overflow semantics. Users who want bounds checking should
1081  // set the appropriate trap.
1082 #if 0
1083  // For floating-point types T, std::numeric_limits<T>::min()
1084  // returns the smallest positive value. IEEE 754 types have a
1085  // sign bit, so the largest-magnitude negative value is the
1086  // negative of the largest-magnitude positive value.
1087  const double minVal = -std::numeric_limits<double>::max ();
1088  const double maxVal = std::numeric_limits<double>::max ();
1089 
1090  // NaN is neither less than nor greater than anything. We just
1091  // let it pass through, per the rules for propagation of silent
1092  // NaN. (Signaling NaN will signal, but that's OK.)
1094  t < minVal || t > maxVal,
1095  std::range_error,
1096  "Teuchos::ValueTypeConversionTraits<double, long double>::safeConvert: "
1097  "Input long double t = " << t << " is out of the valid range [" << minVal
1098  << ", " << maxVal << "] for conversion to double.");
1099 #endif // 0
1100 
1101  return static_cast<double> (t);
1102  }
1103 
1104  static double convert (const long double t) {
1105  return static_cast<double> (t);
1106  }
1107 };
1108 
1109 
1110 //
1111 // * Specializations for conversions from built-in real-valued
1112 // floating-point types (float and double) to build-in integer
1113 // types.
1114 //
1115 
1117 template<>
1118 class ValueTypeConversionTraits<short, double> {
1119 public:
1125  static short convert (const double t) {
1126  // Implicit conversion may cause compiler warnings, but
1127  // static_cast does not.
1128  return static_cast<short> (t);
1129  }
1130 
1132  static short safeConvert (const double t) {
1133  const short minVal = std::numeric_limits<short>::min ();
1134  const short maxVal = std::numeric_limits<short>::max ();
1135 
1136  // Cases:
1137  // 1. sizeof(short) < sizeof(double) == 8
1138  // 2. sizeof(short) == sizeof(double) == 8
1139  // 3. sizeof(short) > sizeof(double) == 8
1140  //
1141  // Overflow when converting from double to short is possible only
1142  // for Case 1. Loss of accuracy (rounding) is possible for Cases
1143  // 2 and 3, but safeConvert() only cares about overflow, not
1144  // rounding. In Case 3, casting minVal or maxVal to double in
1145  // this case could result in overflow. Thus, we only do the test
1146  // for Case 1.
1147  //
1148  // All three cases are legal according to both C++03 and C99.
1149  // However, I (mfh 15 Nov 2012) have never encountered Cases 2 and
1150  // 3.
1151  if (sizeof (short) < sizeof (double)) {
1153  t < minVal || t > maxVal,
1154  std::range_error,
1155  "Teuchos::ValueTypeConversionTraits<short, double>::safeConvert: "
1156  "Input double t = " << t << " is out of the valid range [" << minVal
1157  << ", " << maxVal << "] for conversion to short.");
1158  }
1159  return static_cast<short> (t);
1160  }
1161 };
1162 
1163 
1165 template<>
1166 class ValueTypeConversionTraits<unsigned short, double> {
1167 public:
1169  static unsigned short convert (const double t) {
1170  // Implicit conversion may cause compiler warnings, but
1171  // static_cast does not.
1172  return static_cast<unsigned short> (t);
1173  }
1174 
1176  static unsigned short safeConvert (const double t) {
1177  const unsigned short minVal = 0;
1178  const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1179 
1181  t < minVal || t > maxVal,
1182  std::range_error,
1183  "Teuchos::ValueTypeConversionTraits<unsigned short, double>::safeConvert: "
1184  "Input double t = " << t << " is out of the valid range [" << minVal
1185  << ", " << maxVal << "] for conversion to unsigned short.");
1186 
1187  return static_cast<unsigned short> (t);
1188  }
1189 };
1190 
1191 
1193 template<>
1194 class ValueTypeConversionTraits<int, double> {
1195 public:
1201  static int convert (const double t) {
1202  // Implicit conversion from double to int causes compiler
1203  // warnings, but static_cast does not.
1204  return static_cast<int> (t);
1205  }
1206 
1208  static int safeConvert (const double t) {
1209  const int minVal = std::numeric_limits<int>::min ();
1210  const int maxVal = std::numeric_limits<int>::max ();
1211 
1212  // Cases:
1213  // 1. sizeof(int) < sizeof(double) == 8
1214  // 2. sizeof(int) == sizeof(double) == 8
1215  // 3. sizeof(int) > sizeof(double) == 8
1216  //
1217  // Overflow when converting from double to int is possible only
1218  // for Case 1. Loss of accuracy (rounding) is possible for Cases
1219  // 2 and 3, but safeConvert() only cares about overflow, not
1220  // rounding. Case 3 is quite rare, but casting minVal or maxVal
1221  // to double in this case could result in overflow. Thus, we only
1222  // do the cast for Case 1.
1223  if (sizeof (int) < sizeof (double)) {
1225  t < minVal || t > maxVal,
1226  std::range_error,
1227  "Teuchos::ValueTypeConversionTraits<int, double>::safeConvert: "
1228  "Input double t = " << t << " is out of the valid range [" << minVal
1229  << ", " << maxVal << "] for conversion to int.");
1230  }
1231  return static_cast<int> (t);
1232  }
1233 };
1234 
1235 
1237 template<>
1238 class ValueTypeConversionTraits<unsigned int, double> {
1239 public:
1241  static unsigned int convert (const double t) {
1242  // Implicit conversion may cause compiler warnings, but
1243  // static_cast does not.
1244  return static_cast<unsigned int> (t);
1245  }
1246 
1248  static unsigned int safeConvert (const double t) {
1249  const unsigned int minVal = 0;
1250  const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1251 
1253  t < minVal || t > maxVal,
1254  std::range_error,
1255  "Teuchos::ValueTypeConversionTraits<unsigned int, double>::safeConvert: "
1256  "Input double t = " << t << " is out of the valid range [" << minVal
1257  << ", " << maxVal << "] for conversion to unsigned int.");
1258 
1259  return static_cast<unsigned int> (t);
1260  }
1261 };
1262 
1263 
1265 template<>
1266 class ValueTypeConversionTraits<long, double> {
1267 public:
1269  static long convert (const double t) {
1270  // Implicit conversion may cause compiler warnings, but
1271  // static_cast does not.
1272  return static_cast<long> (t);
1273  }
1274 
1276  static long safeConvert (const double t) {
1277  const long minVal = std::numeric_limits<long>::min ();
1278  const long maxVal = std::numeric_limits<long>::max ();
1279 
1280  // Cases:
1281  // 1. sizeof(long) < sizeof(double) == 8
1282  // 2. sizeof(long) == sizeof(double) == 8
1283  // 3. sizeof(long) > sizeof(double) == 8
1284  //
1285  // Overflow when converting from double to long is possible only
1286  // for Case 1. Loss of accuracy (rounding) is possible for Cases
1287  // 2 and 3, but safeConvert() only cares about overflow, not
1288  // rounding. In Case 3, casting minVal or maxVal to double could
1289  // result in overflow. Thus, we only test for Case 1.
1290  //
1291  // Case 1 is entirely possible, for example on Win64 (an
1292  // implementation of the LLP64 integer model, on which
1293  // sizeof(long) == 4, and sizeof(long long) == sizeof(void*) ==
1294  // 8).
1295  if (sizeof (long) < sizeof (double)) {
1297  t < minVal || t > maxVal,
1298  std::range_error,
1299  "Teuchos::ValueTypeConversionTraits<long, double>::safeConvert: "
1300  "Input double t = " << t << " is out of the valid range [" << minVal
1301  << ", " << maxVal << "] for conversion to long.");
1302  }
1303  return static_cast<long> (t);
1304  }
1305 };
1306 
1307 
1309 template<>
1310 class ValueTypeConversionTraits<unsigned long, double> {
1311 public:
1313  static unsigned long convert (const double t) {
1314  // Implicit conversion may cause compiler warnings, but
1315  // static_cast does not.
1316  return static_cast<unsigned long> (t);
1317  }
1318 
1320  static unsigned long safeConvert (const double t) {
1321  const unsigned long minVal = 0;
1322  const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1323 
1325  t < minVal || t > maxVal,
1326  std::range_error,
1327  "Teuchos::ValueTypeConversionTraits<unsigned long, double>::safeConvert: "
1328  "Input double t = " << t << " is out of the valid range [" << minVal
1329  << ", " << maxVal << "] for conversion to unsigned long.");
1330 
1331  return static_cast<unsigned long> (t);
1332  }
1333 };
1334 
1335 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
1336 
1338 template<>
1339 class ValueTypeConversionTraits<long long, double> {
1340 public:
1342  static long long convert (const double t) {
1343  // Implicit conversion may cause compiler warnings, but
1344  // static_cast does not.
1345  return static_cast<long long> (t);
1346  }
1347 
1349  static long safeConvert (const double t) {
1350  // Cases:
1351  // 1. sizeof(long long) < sizeof(double) == 8
1352  // 2. sizeof(long long) == sizeof(double) == 8
1353  // 3. sizeof(long long) > sizeof(double) == 8
1354  //
1355  // C99 (which defines long long) prohibits Case 1. Case 2 could
1356  // result in loss of accuracy (rounding), but safeConvert() only
1357  // cares about overflow, not rounding. In Case 3, casting minVal
1358  // or maxVal to double could result in overflow. Thus, we don't
1359  // need to check anything here.
1360  return static_cast<long long> (t);
1361  }
1362 };
1363 
1364 
1366 template<>
1367 class ValueTypeConversionTraits<unsigned long long, double> {
1368 public:
1370  static unsigned long long convert (const double t) {
1371  // Implicit conversion may cause compiler warnings, but
1372  // static_cast does not.
1373  return static_cast<unsigned long long> (t);
1374  }
1375 
1377  static unsigned long long safeConvert (const double t) {
1378  const unsigned long long minVal = 0; // unsigned, so min value is 0.
1379  const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1380 
1382  t < minVal || t > maxVal,
1383  std::range_error,
1384  "Teuchos::ValueTypeConversionTraits<unsigned long long, double>::safeConvert: "
1385  "Input double t = " << t << " is out of the valid range [" << minVal
1386  << ", " << maxVal << "] for conversion to unsigned long long.");
1387 
1388  return static_cast<unsigned long long> (t);
1389  }
1390 };
1391 
1392 #endif // HAVE_TEUCHOS_LONG_LONG_INT
1393 
1394 
1396 template<>
1397 class ValueTypeConversionTraits<short, float> {
1398 public:
1404  static short convert (const float t) {
1405  // Implicit conversion may cause compiler warnings, but
1406  // static_cast does not.
1407  return static_cast<short> (t);
1408  }
1409 
1411  static short safeConvert (const float t) {
1412  const short minVal = std::numeric_limits<short>::min ();
1413  const short maxVal = std::numeric_limits<short>::max ();
1414 
1415  // Cases:
1416  // 1. sizeof(short) < sizeof(float) == 4
1417  // 2. sizeof(short) == sizeof(float) == 4
1418  // 3. sizeof(short) > sizeof(float) == 4
1419  //
1420  // Overflow when converting from float to short is possible only
1421  // for Case 1. Loss of accuracy (rounding) is possible for Cases
1422  // 2 and 3, but safeConvert() only cares about overflow, not
1423  // rounding. In Case 3, casting minVal or maxVal to float in this
1424  // case could result in overflow. Thus, we only do the test for
1425  // Case 1.
1426  //
1427  // All three cases are legal according to both C++03 and C99. I
1428  // (mfh 15 Nov 2012) think Case 1 is the most common, but Case 2
1429  // is certainly reasonable. (For example, some hardware prefers
1430  // to work only with 32-bit words, so _every_ built-in type has
1431  // size a multiple of 4 bytes.)
1432  if (sizeof (short) < sizeof (float)) {
1434  t < minVal || t > maxVal,
1435  std::range_error,
1436  "Teuchos::ValueTypeConversionTraits<short, float>::safeConvert: "
1437  "Input float t = " << t << " is out of the valid range [" << minVal
1438  << ", " << maxVal << "] for conversion to short.");
1439  }
1440 
1441  return static_cast<short> (t);
1442  }
1443 };
1444 
1445 
1447 template<>
1448 class ValueTypeConversionTraits<unsigned short, float> {
1449 public:
1451  static unsigned short convert (const float t) {
1452  // Implicit conversion may cause compiler warnings, but
1453  // static_cast does not.
1454  return static_cast<unsigned short> (t);
1455  }
1456 
1458  static unsigned short safeConvert (const float t) {
1459  const unsigned short minVal = 0;
1460  const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1461 
1463  t < minVal || t > maxVal,
1464  std::range_error,
1465  "Teuchos::ValueTypeConversionTraits<unsigned short, float>::safeConvert: "
1466  "Input float t = " << t << " is out of the valid range [" << minVal
1467  << ", " << maxVal << "] for conversion to unsigned short.");
1468 
1469  return static_cast<unsigned short> (t);
1470  }
1471 };
1472 
1473 
1475 template<>
1476 class ValueTypeConversionTraits<int, float> {
1477 public:
1479  static int convert (const float t) {
1480  // Implicit conversion from float to int may cause compiler
1481  // warnings, but static_cast does not. Overflow here would mean
1482  // that sizeof(int) < sizeof(float), which is legal, but unlikely
1483  // on platforms of interest.
1484  return static_cast<int> (t);
1485  }
1486 
1488  static int safeConvert (const float t) {
1489  const int minVal = std::numeric_limits<int>::min ();
1490  const int maxVal = std::numeric_limits<int>::max ();
1491 
1492  // Cases:
1493  // 1. sizeof(int) < sizeof(float) == 4
1494  // 2. sizeof(int) == sizeof(float) == 4
1495  // 3. sizeof(int) > sizeof(float) == 4
1496  //
1497  // Overflow when converting from float to int is possible only for
1498  // Case 1. Loss of accuracy (rounding) is possible for Cases 2
1499  // and 3, but safeConvert() only cares about overflow, not
1500  // rounding. Case 3 is rare, but casting minVal or maxVal to
1501  // float in this case could result in loss of accuracy
1502  // (sizeof(int) == 8 or 16) or overflow (sizeof(int) > 16). Thus,
1503  // we only do the test for Case 1.
1504  if (sizeof (int) < sizeof (float)) {
1506  t < minVal || t > maxVal,
1507  std::range_error,
1508  "Teuchos::ValueTypeConversionTraits<int, float>::safeConvert: "
1509  "Input float t = " << t << " is out of the valid range ["
1510  << minVal << ", " << maxVal << "] for conversion to int.");
1511  }
1512  return static_cast<int> (t);
1513  }
1514 };
1515 
1516 
1518 template<>
1519 class ValueTypeConversionTraits<unsigned int, float> {
1520 public:
1522  static unsigned int convert (const float t) {
1523  // Implicit conversion from float to unsigned int may cause
1524  // compiler warnings, but static_cast does not.
1525  return static_cast<unsigned int> (t);
1526  }
1527 
1529  static unsigned int safeConvert (const float t) {
1530  const unsigned int minVal = 0; // Had better be, since it's unsigned.
1531  const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1532 
1534  t < minVal || t > maxVal,
1535  std::range_error,
1536  "Teuchos::ValueTypeConversionTraits<unsigned int, float>::safeConvert: "
1537  "Input double t = " << t << " is out of the valid range [" << minVal
1538  << ", " << maxVal << "] for conversion to unsigned int.");
1539 
1540  return static_cast<unsigned int> (t);
1541  }
1542 };
1543 
1544 
1546 template<>
1547 class ValueTypeConversionTraits<long, float> {
1548 public:
1550  static long convert (const float t) {
1551  // Implicit conversion from float to long may cause compiler
1552  // warnings, but static_cast does not. Overflow here would mean
1553  // that sizeof(long) < sizeof(float), which is legal, but unlikely
1554  // on platforms of longerest.
1555  return static_cast<long> (t);
1556  }
1557 
1559  static long safeConvert (const float t) {
1560  const long minVal = std::numeric_limits<long>::min ();
1561  const long maxVal = std::numeric_limits<long>::max ();
1562 
1563  // Cases:
1564  // 1. sizeof(long) < sizeof(float) == 4
1565  // 2. sizeof(long) == sizeof(float) == 4
1566  // 3. sizeof(long) > sizeof(float) == 4
1567  //
1568  // Overflow when converting from float to long is possible only
1569  // for Case 1. Loss of accuracy (rounding) is possible for Cases
1570  // 2 and 3, but safeConvert() only cares about overflow, not
1571  // rounding. Casting minVal or maxVal to double in Case 3 could
1572  // result in overflow. Thus, we only do the cast for Case 1.
1573  //
1574  // I've never encountered a Case 1 platform (mfh 14 Nov 2012).
1575  // C99 actually forbids it, though I don't think a valid C++
1576  // compiler (for version C++03 of the language standard) needs to
1577  // implement C99 (mfh 14 Nov 2012). Case 2 occurs in Win64
1578  // (64-bit Windows) and other implementations of (I32L32)LLP64.
1579  // Case 3 is common (e.g., in the (I32)LP64 integer model of
1580  // GNU/Linux and other operating systems).
1581  if (sizeof (long) < sizeof (float)) {
1583  t < minVal || t > maxVal,
1584  std::range_error,
1585  "Teuchos::ValueTypeConversionTraits<long, float>::safeConvert: "
1586  "Input float t = " << t << " is out of the valid range ["
1587  << minVal << ", " << maxVal << "] for conversion to long.");
1588  }
1589  return static_cast<long> (t);
1590  }
1591 };
1592 
1593 
1595 template<>
1596 class ValueTypeConversionTraits<unsigned long, float> {
1597 public:
1599  static unsigned long convert (const float t) {
1600  // Implicit conversion from float to unsigned long may cause
1601  // compiler warnings, but static_cast does not.
1602  return static_cast<unsigned long> (t);
1603  }
1604 
1606  static unsigned long safeConvert (const float t) {
1607  const unsigned long minVal = 0; // Had better be, since it's unsigned.
1608  const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1609 
1611  t < minVal || t > maxVal,
1612  std::range_error,
1613  "Teuchos::ValueTypeConversionTraits<unsigned long, float>::safeConvert: "
1614  << "Input float t = " << t << " is out of the valid range [" << minVal
1615  << ", " << maxVal << "] for conversion to unsigned long.");
1616 
1617  return static_cast<unsigned long> (t);
1618  }
1619 };
1620 
1621 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
1622 
1624 template<>
1625 class ValueTypeConversionTraits<long long, float> {
1626 public:
1628  static long long convert (const float t) {
1629  return static_cast<long long> (t);
1630  }
1631 
1633  static long long safeConvert (const float t) {
1634  // The C99 standard (Section 5.2.4.2.1) actually requires
1635  // sizeof(long long) >= 64, so overflow is impossible.
1636  return static_cast<long long> (t);
1637  }
1638 };
1639 
1640 
1642 template<>
1643 class ValueTypeConversionTraits<unsigned long long, float> {
1644 public:
1646  static unsigned long long convert (const float t) {
1647  return static_cast<unsigned long long> (t);
1648  }
1649 
1651  static unsigned long long safeConvert (const float t) {
1652  const unsigned long long minVal = 0; // unsigned, so min value is 0.
1653  const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1654 
1656  t < minVal || t > maxVal,
1657  std::range_error,
1658  "Teuchos::ValueTypeConversionTraits<unsigned long long, float>::safeConvert: "
1659  "Input float t = " << t << " is out of the valid range [" << minVal
1660  << ", " << maxVal << "] for conversion to unsigned long long.");
1661 
1662  return static_cast<unsigned long long> (t);
1663  }
1664 };
1665 
1666 #endif // HAVE_TEUCHOS_LONG_LONG_INT
1667 
1668 //
1669 // * Specializations for conversions between a unsigned built-in
1670 // integer type and the signed version of the same type (either
1671 // direction).
1672 //
1673 
1674 namespace {
1675 // Implementation of conversion from an unsigned built-in integer
1676 // type, to an signed built-in integer type with the same number of
1677 // bits.
1678 template<class SignedIntType, class UnsignedIntType>
1679 class UnsignedToSignedValueTypeConversionTraits {
1680 public:
1688  static SignedIntType convert (const UnsignedIntType t) {
1689  // Implicit conversion may cause compiler warnings, but
1690  // static_cast does not.
1691  return static_cast<SignedIntType> (t);
1692  }
1693 
1695  static SignedIntType safeConvert (const UnsignedIntType t) {
1697  const SignedIntType maxSigned = std::numeric_limits<SignedIntType>::max ();
1698 
1699  // SignedIntType and UnsignedIntType have the same number of bits,
1700  // so it suffices (via two's complement arithmetic) to check
1701  // whether the cast turned a positive number negative.
1702  const SignedIntType signedVal = static_cast<SignedIntType> (t);
1704  signedVal < 0,
1705  std::range_error,
1706  "Teuchos::ValueTypeConversionTraits<" << TypeNameTraits<SignedIntType>::name ()
1707  << ", " << TypeNameTraits<UnsignedIntType>::name () << ">::safeConvert: "
1708  "Input " << TypeNameTraits<UnsignedIntType>::name () << " t = " << t
1709  << " is out of the valid range [0, " << ", " << maxSigned
1710  << "] for conversion to " << TypeNameTraits<SignedIntType>::name () << ".");
1711  return signedVal;
1712  }
1713 };
1714 
1715 
1716 // Implementation of conversion from a signed built-in integer type,
1717 // to an unsigned built-in integer type with the same number of bits.
1718 template<class UnsignedIntType, class SignedIntType>
1719 class SignedToUnsignedValueTypeConversionTraits {
1720 public:
1722  static UnsignedIntType convert (const SignedIntType t) {
1723  // Implicit conversion may cause compiler warnings, but
1724  // static_cast does not.
1725  return static_cast<UnsignedIntType> (t);
1726  }
1727 
1729  static UnsignedIntType safeConvert (const SignedIntType t) {
1731 
1732  // SignedIntType and UnsignedIntType have the same number of bits,
1733  // so it suffices (via two's complement arithmetic) to check
1734  // whether the input is negative.
1736  t < static_cast<SignedIntType> (0),
1737  std::range_error,
1738  "Teuchos::ValueTypeConversionTraits<" << TypeNameTraits<UnsignedIntType>::name ()
1739  << ", " << TypeNameTraits<SignedIntType>::name () << ">::safeConvert: "
1740  "Input " << TypeNameTraits<SignedIntType>::name () << " t = " << t
1741  << " is negative, so it cannot be correctly converted to the unsigned type "
1742  << TypeNameTraits<UnsignedIntType>::name () << ".");
1743 
1744  return static_cast<UnsignedIntType> (t);
1745  }
1746 };
1747 
1748 } // namespace (anonymous)
1749 
1750 
1752 template<>
1753 class ValueTypeConversionTraits<short, unsigned short> {
1754 public:
1755  static short convert (const unsigned short t) {
1756  return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::convert (t);
1757  }
1758 
1759  static short safeConvert (const unsigned short t) {
1760  return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::safeConvert (t);
1761  }
1762 };
1763 
1764 
1766 template<>
1767 class ValueTypeConversionTraits<unsigned short, short> {
1768 public:
1769  static unsigned short convert (const short t) {
1770  return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::convert (t);
1771  }
1772 
1773  static unsigned short safeConvert (const short t) {
1774  return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::safeConvert (t);
1775  }
1776 };
1777 
1778 
1780 template<>
1781 class ValueTypeConversionTraits<int, unsigned int> {
1782 public:
1783  static int convert (const unsigned int t) {
1784  return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::convert (t);
1785  }
1786 
1787  static int safeConvert (const unsigned int t) {
1788  return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::safeConvert (t);
1789  }
1790 };
1791 
1792 
1794 template<>
1795 class ValueTypeConversionTraits<unsigned int, int> {
1796 public:
1797  static unsigned int convert (const int t) {
1798  return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::convert (t);
1799  }
1800 
1801  static unsigned int safeConvert (const int t) {
1802  return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::safeConvert (t);
1803  }
1804 };
1805 
1806 
1808 template<>
1809 class ValueTypeConversionTraits<long, unsigned long> {
1810 public:
1811  static long convert (const unsigned long t) {
1812  return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::convert (t);
1813  }
1814 
1815  static long safeConvert (const unsigned long t) {
1816  return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::safeConvert (t);
1817  }
1818 };
1819 
1820 
1822 template<>
1823 class ValueTypeConversionTraits<unsigned long, long> {
1824 public:
1825  static unsigned long convert (const long t) {
1826  return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::convert (t);
1827  }
1828 
1829  static unsigned long safeConvert (const long t) {
1830  return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::safeConvert (t);
1831  }
1832 };
1833 
1834 
1835 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
1836 
1838 template<>
1839 class ValueTypeConversionTraits<long long, unsigned long long> {
1840 public:
1841  static long long convert (const unsigned long long t) {
1842  return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::convert (t);
1843  }
1844 
1845  static long long safeConvert (const unsigned long long t) {
1846  return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::safeConvert (t);
1847  }
1848 };
1849 
1850 
1852 template<>
1853 class ValueTypeConversionTraits<unsigned long long, long long> {
1854 public:
1855  static unsigned long long convert (const long long t) {
1856  return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::convert (t);
1857  }
1858 
1859  static unsigned long long safeConvert (const long long t) {
1860  return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::safeConvert (t);
1861  }
1862 };
1863 
1864 #endif // HAVE_TEUCHOS_LONG_LONG_INT
1865 
1866 //
1867 // * Specializations for conversions between different built-in
1868 // integer types.
1869 //
1870 
1872 template<>
1873 class ValueTypeConversionTraits<short, int> {
1874 public:
1880  static short convert (const int t) {
1881  // Implicit conversion may cause compiler warnings, but
1882  // static_cast does not.
1883  return static_cast<short> (t);
1884  }
1885 
1887  static short safeConvert (const int t) {
1888  const short minShort = std::numeric_limits<short>::min ();
1889  const short maxShort = std::numeric_limits<short>::max ();
1890 
1891  // Casting from short to int never overflows, since the C++
1892  // standard guarantees that sizeof (short) <= sizeof (int).
1894  t < static_cast<int> (minShort) ||
1895  t > static_cast<int> (maxShort),
1896  std::range_error,
1897  "Teuchos::ValueTypeConversionTraits<short, int>::safeConvert: "
1898  "Input int t = " << t << " is out of the valid range [" << minShort
1899  << ", " << maxShort << "] for conversion to short.");
1900 
1901  return static_cast<short> (t);
1902  }
1903 };
1904 
1905 
1907 template<>
1908 class ValueTypeConversionTraits<short, long> {
1909 public:
1915  static short convert (const long t) {
1916  // Implicit conversion may cause compiler warnings, but
1917  // static_cast does not.
1918  return static_cast<short> (t);
1919  }
1920 
1922  static short safeConvert (const long t) {
1923  const short minShort = std::numeric_limits<short>::min ();
1924  const short maxShort = std::numeric_limits<short>::max ();
1925 
1926  // Casting from short to long never overflows, since the C++
1927  // standard guarantees that sizeof (short) <= sizeof (long).
1929  t < static_cast<long> (minShort) ||
1930  t > static_cast<long> (maxShort),
1931  std::range_error,
1932  "Teuchos::ValueTypeConversionTraits<short, long>::safeConvert: "
1933  "Input long t = " << t << " is out of the valid range [" << minShort
1934  << ", " << maxShort << "] for conversion to short.");
1935 
1936  return static_cast<short> (t);
1937  }
1938 };
1939 
1940 
1942 template<>
1943 class ValueTypeConversionTraits<int, long> {
1944 public:
1950  static int convert (const long t) {
1951  // Implicit conversion from long to int may cause compiler
1952  // warnings, but static_cast does not.
1953  return static_cast<int> (t);
1954  }
1955 
1957  static int safeConvert (const long t) {
1958  const int minInt = std::numeric_limits<int>::min ();
1959  const int maxInt = std::numeric_limits<int>::max ();
1960 
1961  // Casting from int to long never overflows, since the C++
1962  // standard guarantees that sizeof (int) <= sizeof (long).
1964  t < static_cast<long> (minInt) ||
1965  t > static_cast<long> (maxInt),
1966  std::range_error,
1967  "Teuchos::ValueTypeConversionTraits<int, long>::safeConvert: "
1968  "Input long t = " << t << " is out of the valid range [" << minInt
1969  << ", " << maxInt << "] for conversion to int.");
1970 
1971  // Implicit conversion from long to int may cause compiler
1972  // warnings, but static_cast does not.
1973  return static_cast<int> (t);
1974  }
1975 };
1976 
1977 
1979 template<>
1980 class ValueTypeConversionTraits<int, unsigned long> {
1981 public:
1988  static int convert (const unsigned long t) {
1989  // Implicit conversion may cause compiler warnings, but
1990  // static_cast does not.
1991  return static_cast<int> (t);
1992  }
1993 
1995  static int safeConvert (const unsigned long t) {
1996  const int minInt = std::numeric_limits<int>::min ();
1997  const int maxInt = std::numeric_limits<int>::max ();
1998 
1999  // On some platforms, sizeof(int) == sizeof(long). (This is the
2000  // "LLP64" model of Win64, which aims for backwards compatibility
2001  // with 32-bit code by making sizeof(int) == sizeof(long) == 4.)
2002  // If this is the case, then we can't safely cast unsigned long to
2003  // int, or unsigned int to long, because values with the most
2004  // significant bit set will overflow to negative values.
2005 
2006  // The C++ standard promises that sizeof (int) <= sizeof (unsigned
2007  // long). We use #if with INT_MAX and LONG_MAX to test for this,
2008  // rather than if statements, in order to avoid a compiler
2009  // warning. Thanks to Jeremie Gaidamour (13 Nov 2012) for letting
2010  // me know about the warning.
2011 #if INT_MAX == LONG_MAX
2012  // The two types have the same number of bits. Thus,
2013  // two's-complement arithmetic means that if casting from unsigned
2014  // long to int results in a negative number, it overflowed.
2015  // Otherwise, it didn't overflow (same number of bits).
2017  static_cast<int> (t) < static_cast<int> (0),
2018  std::range_error,
2019  "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: "
2020  "Input unsigned long t = " << t << " is out of the valid range ["
2021  << minInt << ", " << maxInt << "] for conversion to int.");
2022 #else // INT_MAX < LONG_MAX
2023  // t is unsigned, so it is >= 0 by definition.
2024  // Casting from int to unsigned long won't overflow in this case.
2026  t > static_cast<unsigned long> (maxInt),
2027  std::range_error,
2028  "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: "
2029  "Input unsigned long t = " << t << " is out of the valid range ["
2030  << minInt << ", " << maxInt << "] for conversion to int. An unchecked "
2031  "cast would have resulted in " << static_cast<int> (t) << ".");
2032 #endif // INT_MAX == LONG_MAX
2033 
2034  // Implicit conversion from unsigned long to int may cause
2035  // compiler warnings, but static_cast does not.
2036  return static_cast<int> (t);
2037  }
2038 };
2039 
2040 
2042 template<>
2043 class ValueTypeConversionTraits<long, unsigned int> {
2044 public:
2052  static long convert (const unsigned int t) {
2053  // Implicit conversion may cause compiler warnings, but
2054  // static_cast does not.
2055  return static_cast<long> (t);
2056  }
2057 
2059  static long safeConvert (const unsigned int t) {
2060  // On some platforms, sizeof(int) == sizeof(long). (This is the
2061  // "LLP64" model of Win64, which aims for backwards compatibility
2062  // with 32-bit code by making sizeof(int) == sizeof(long) == 4.)
2063  // If this is the case, then we can't safely cast unsigned long to
2064  // int, or unsigned int to long, because values with the most
2065  // significant bit set will overflow to negative values.
2066 
2067  // The C++ standard promises that sizeof (unsigned int) <= sizeof
2068  // (long). If strictly less, then the conversion won't overflow.
2069  // We protect the test with an #ifdef ... #endif to avoid compiler
2070  // warnings like the following: "warning: comparison is always
2071  // false due to limited range of data type".
2072 #if UINT_MAX == LONG_MAX
2073  const long minLong = std::numeric_limits<long>::min ();
2074  const long maxLong = std::numeric_limits<long>::max ();
2075 
2076  // The two types have the same number of bits. Thus,
2077  // two's-complement arithmetic means that if casting from
2078  // unsigned int to long results in a negative number, it
2079  // overflowed. Otherwise, it didn't overflow (same number of
2080  // bits).
2082  static_cast<long> (t) < static_cast<long> (0),
2083  std::range_error,
2084  "Teuchos::ValueTypeConversionTraits<long, unsigned int>::safeConvert: "
2085  "Input unsigned int t = " << t << " is out of the valid range ["
2086  << minLong << ", " << maxLong << "] for conversion to long.");
2087 #endif // UINT_MAX == LONG_MAX
2088 
2089  return static_cast<long> (t);
2090  }
2091 };
2092 
2093 
2095 template<>
2096 class ValueTypeConversionTraits<unsigned int, long> {
2097 public:
2104  static unsigned int convert (const long t) {
2105  // Implicit conversion from long to unsigned int may cause
2106  // compiler warnings, but static_cast does not.
2107  return static_cast<unsigned int> (t);
2108  }
2109 
2111  static unsigned int safeConvert (const long t) {
2112  // On some platforms, sizeof(int) == sizeof(long). (This is the
2113  // "LLP64" model of Win64, which aims for backwards compatibility
2114  // with 32-bit code by making sizeof(int) == sizeof(long) == 4.)
2115  // In this case, conversion from long to unsigned int can't
2116  // overflow.
2117 
2118  // The C++ standard promises that sizeof (unsigned int) <= sizeof (long).
2119  if (sizeof (unsigned int) < sizeof (long)) {
2120  const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2121 
2123  t < static_cast<long> (0) || t > static_cast<long> (maxInt),
2124  std::range_error,
2125  "Teuchos::ValueTypeConversionTraits<unsigned int, long>::safeConvert: "
2126  "Input long t = " << t << " is out of the valid range [0, "
2127  << maxInt << "] for conversion to unsigned int.");
2128  }
2129  // Implicit conversion from long to unsigned int may cause
2130  // compiler warnings, but static_cast does not.
2131  return static_cast<unsigned int> (t);
2132  }
2133 };
2134 
2135 
2137 template<>
2138 class ValueTypeConversionTraits<unsigned int, unsigned long> {
2139 public:
2146  static unsigned int convert (const unsigned long t) {
2147  // Implicit conversion from unsigned long to unsigned int may cause
2148  // compiler warnings, but static_cast does not.
2149  return static_cast<unsigned int> (t);
2150  }
2151 
2153  static unsigned int safeConvert (const unsigned long t) {
2154  const unsigned int minInt = 0; // Had better be, since it's unsigned.
2155  const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2156 
2157  // t >= 0 by definition, because it is unsigned.
2159  t > static_cast<unsigned long> (maxInt),
2160  std::range_error,
2161  "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long>::safeConvert: "
2162  "Input unsigned long t = " << t << " is out of the valid range [" << minInt
2163  << ", " << maxInt << "] for conversion to unsigned int.");
2164 
2165  // Implicit conversion from unsigned long to unsigned int may
2166  // cause compiler warnings, but static_cast does not.
2167  return static_cast<unsigned int> (t);
2168  }
2169 };
2170 
2171 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
2172 
2174 template<>
2175 class ValueTypeConversionTraits<int, long long> {
2176 public:
2182  static int convert (const long long t) {
2183  // Implicit conversion from long long to int may cause compiler
2184  // warnings, but static_cast does not.
2185  return static_cast<int> (t);
2186  }
2187 
2189  static int safeConvert (const long long t) {
2190  const int minInt = std::numeric_limits<int>::min ();
2191  const int maxInt = std::numeric_limits<int>::max ();
2192 
2194  t < static_cast<long long> (minInt) ||
2195  t > static_cast<long long> (maxInt),
2196  std::range_error,
2197  "Teuchos::ValueTypeConversionTraits<int, long long>::safeConvert: "
2198  "Input long long t = " << t << " is out of the valid range [" << minInt
2199  << ", " << maxInt << "] for conversion to int.");
2200 
2201  // Implicit conversion from long long to int may cause compiler
2202  // warnings, but static_cast does not.
2203  return static_cast<int> (t);
2204  }
2205 };
2206 
2207 
2209 template<>
2210 class ValueTypeConversionTraits<unsigned int, long long> {
2211 public:
2218  static unsigned int convert (const long long t) {
2219  // Implicit conversion from long long to unsigned int may cause
2220  // compiler warnings, but static_cast does not.
2221  return static_cast<unsigned int> (t);
2222  }
2223 
2225  static unsigned int safeConvert (const long long t) {
2226  const unsigned int minInt = 0; // Had better be, because it's unsigned.
2227  const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2228 
2230  t < static_cast<long long> (minInt) || t > static_cast<long long> (maxInt),
2231  std::range_error,
2232  "Teuchos::ValueTypeConversionTraits<unsigned int, long long>::safeConvert: "
2233  "Input long long t = " << t << " is out of the valid range [" << minInt
2234  << ", " << maxInt << "] for conversion to unsigned int.");
2235 
2236  // Implicit conversion from long long to unsigned int may cause
2237  // compiler warnings, but static_cast does not.
2238  return static_cast<unsigned int> (t);
2239  }
2240 };
2241 
2242 
2244 template<>
2245 class ValueTypeConversionTraits<int, unsigned long long> {
2246 public:
2252  static int convert (const unsigned long long t) {
2253  // Implicit conversion from unsigned long long to int may cause
2254  // compiler warnings, but static_cast does not.
2255  return static_cast<int> (t);
2256  }
2257 
2259  static int safeConvert (const unsigned long long t) {
2260  const int minInt = std::numeric_limits<int>::min ();
2261  const int maxInt = std::numeric_limits<int>::max ();
2262 
2263  // t >= 0 by definition, because it is unsigned.
2265  t > static_cast<unsigned long long> (maxInt),
2266  std::invalid_argument,
2267  "Teuchos::ValueTypeConversionTraits<int, unsigned long long>::safeConvert: "
2268  "Input unsigned long long t = " << t << " is out of the valid range [" << minInt
2269  << ", " << maxInt << "] for conversion to int.");
2270 
2271  // Implicit conversion from unsigned long long to int may cause
2272  // compiler warnings, but static_cast does not.
2273  return static_cast<int> (t);
2274  }
2275 };
2276 
2277 
2279 template<>
2280 class ValueTypeConversionTraits<unsigned int, unsigned long long> {
2281 public:
2288  static unsigned int convert (const unsigned long long t) {
2289  // Implicit conversion from unsigned long long to unsigned int may
2290  // cause compiler warnings, but static_cast does not.
2291  return static_cast<unsigned int> (t);
2292  }
2293 
2295  static unsigned int safeConvert (const unsigned long long t) {
2296  const unsigned int minInt = 0; // Had better be, since it's unsigned.
2297  const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2298 
2299  // t >= 0 by definition, because it is unsigned.
2301  t > static_cast<unsigned long long> (maxInt),
2302  std::invalid_argument,
2303  "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long long>::safeConvert: "
2304  "Input unsigned long long t = " << t << " is out of the valid range [" << minInt
2305  << ", " << maxInt << "] for conversion to unsigned int.");
2306 
2307  // Implicit conversion from unsigned long long to unsigned int may
2308  // cause compiler warnings, but static_cast does not.
2309  return static_cast<unsigned int> (t);
2310  }
2311 };
2312 
2313 #endif // HAVE_TEUCHOS_LONG_LONG_INT
2314 
2315 //
2316 // * Conversions from built-in integer types to built-in real-valued
2317 // floating-point types.
2318 //
2319 
2320 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
2321 
2323 template<>
2324 class ValueTypeConversionTraits<float, long long> {
2325 public:
2331  static float convert (const long long t) {
2332  // Implicit conversion from long long to float may cause compiler
2333  // warnings, but static_cast does not.
2334  return static_cast<float> (t);
2335  }
2336 
2338  static float safeConvert (const long long t) {
2339  // std::numeric_limits<float>::min() gives the minimum _positive_
2340  // normalized value of type float. IEEE 754 floating-point values
2341  // can change sign just by flipping the sign bit, so the "most
2342  // negative" finite float is just the negative of the "most
2343  // positive" finite float.
2344  const float minFloat = -std::numeric_limits<float>::max ();
2345  const float maxFloat = std::numeric_limits<float>::max ();
2346 
2347  // mfh 16 Nov 2012: On my platform (gcc 4.7.2, Red Hat Linux 5,
2348  // Intel x86_64), first casting [minFloat,maxFloat] to long long
2349  // (so that the comparison only compares long long values)
2350  // gives different results in the comparison below than just
2351  // comparing t (as a long long) with minFloat and maxFloat. It
2352  // doesn't matter whether you use static_cast<long long> (...) or
2353  // (long long) (...) to do the cast: the original float interval
2354  // of [-3.40282e+38, 3.40282e+38] becomes [-9223372036854775808,
2355  // -9223372036854775808], which is obviously wrong.
2357  t < minFloat || t > maxFloat,
2358  std::range_error,
2359  "Teuchos::ValueTypeConversionTraits<float, long long>::safeConvert: "
2360  "Input long long t = " << t << " is out of the valid range [" << minFloat
2361  << ", " << maxFloat << "] for conversion to float.");
2362 
2363  // Implicit conversion from long long to float may cause compiler
2364  // warnings, but static_cast does not.
2365  return static_cast<float> (t);
2366  }
2367 };
2368 
2369 
2371 template<>
2372 class ValueTypeConversionTraits<float, unsigned long long> {
2373 public:
2379  static float convert (const unsigned long long t) {
2380  // Implicit conversion from unsigned long long to float may cause
2381  // compiler warnings, but static_cast does not.
2382  return static_cast<float> (t);
2383  }
2384 
2386  static float safeConvert (const unsigned long long t) {
2387  // std::numeric_limits<float>::min() gives the minimum _positive_
2388  // normalized value of type float. IEEE 754 floating-point values
2389  // can change sign just by flipping the sign bit, so the "most
2390  // negative" finite float is just the negative of the "most
2391  // positive" finite float.
2392  const float minFloat = -std::numeric_limits<float>::max ();
2393  const float maxFloat = std::numeric_limits<float>::max ();
2394 
2395  // t >= 0 by definition, because it is unsigned.
2396  //
2397  // mfh 16 Nov 2012: See my note above on the <float, long long>
2398  // specialization that explains why I don't cast maxFloat to
2399  // unsigned long long here.
2401  t > maxFloat,
2402  std::invalid_argument,
2403  "Teuchos::ValueTypeConversionTraits<float, unsigned long long>::safeConvert: "
2404  "Input unsigned long long t = " << t << " is out of the valid range [" << minFloat
2405  << ", " << maxFloat << "] for conversion to float.");
2406 
2407  // Implicit conversion from unsigned long long to float may cause
2408  // compiler warnings, but static_cast does not.
2409  return static_cast<float> (t);
2410  }
2411 };
2412 
2413 #endif // HAVE_TEUCHOS_LONG_LONG_INT
2414 
2415 //
2416 // * Other conversions
2417 //
2418 
2420 template<int N>
2421 class ValueTypeConversionTraits<std::string, char[N]> {
2422 public:
2423  static std::string convert( const char t[] )
2424  { return std::string(t); }
2425  static std::string safeConvert( const char t[] )
2426  { return std::string(t); }
2427 };
2428 
2429 //
2430 // * Conversions from built-in integer types to std::complex<T>.
2431 //
2432 
2433 #ifdef HAVE_TEUCHOS_COMPLEX
2434 
2436 template<class RealType>
2437 class ValueTypeConversionTraits<std::complex<RealType>, short> {
2438 public:
2439  inline static std::complex<RealType> convert (const short t) {
2440  // Let RealType handle the conversion of the zero imaginary part.
2441  return std::complex<RealType> (t, as<RealType> (0));
2442  }
2443  static std::complex<RealType> safeConvert (const short t) {
2444  // Let RealType handle the conversion of the zero imaginary part.
2445  return std::complex<RealType> (t, asSafe<RealType> (0));
2446  }
2447 };
2448 
2450 template<class RealType>
2451 class ValueTypeConversionTraits<std::complex<RealType>, unsigned short> {
2452 public:
2453  inline static std::complex<RealType> convert (const unsigned short t) {
2454  // Let RealType handle the conversion of the zero imaginary part.
2455  return std::complex<RealType> (t, as<RealType> (0));
2456  }
2457  static std::complex<RealType> safeConvert (const unsigned short t) {
2458  // Let RealType handle the conversion of the zero imaginary part.
2459  return std::complex<RealType> (t, asSafe<RealType> (0));
2460  }
2461 };
2462 
2464 template<class RealType>
2465 class ValueTypeConversionTraits<std::complex<RealType>, int> {
2466 public:
2467  inline static std::complex<RealType> convert (const int t) {
2468  // Let RealType handle the conversion of the zero imaginary part.
2469  return std::complex<RealType> (t, as<RealType> (0));
2470  }
2471  static std::complex<RealType> safeConvert (const int t) {
2472  // Let RealType handle the conversion of the zero imaginary part.
2473  return std::complex<RealType> (t, asSafe<RealType> (0));
2474  }
2475 };
2476 
2478 template<class RealType>
2479 class ValueTypeConversionTraits<std::complex<RealType>, unsigned int> {
2480 public:
2481  inline static std::complex<RealType> convert (const unsigned int t) {
2482  // Let RealType handle the conversion of the zero imaginary part.
2483  return std::complex<RealType> (t, as<RealType> (0));
2484  }
2485  static std::complex<RealType> safeConvert (const unsigned int t) {
2486  // Let RealType handle the conversion of the zero imaginary part.
2487  return std::complex<RealType> (t, asSafe<RealType> (0));
2488  }
2489 };
2490 
2492 template<class RealType>
2493 class ValueTypeConversionTraits<std::complex<RealType>, long> {
2494 public:
2495  inline static std::complex<RealType> convert (const long t) {
2496  // Let RealType handle the conversion of the zero imaginary part.
2497  return std::complex<RealType> (t, as<RealType> (0));
2498  }
2499  static std::complex<RealType> safeConvert (const long t) {
2500  // Let RealType handle the conversion of the zero imaginary part.
2501  return std::complex<RealType> (t, asSafe<RealType> (0));
2502  }
2503 };
2504 
2506 template<class RealType>
2507 class ValueTypeConversionTraits<std::complex<RealType>, unsigned long> {
2508 public:
2509  inline static std::complex<RealType> convert (const unsigned long t) {
2510  // Let RealType handle the conversion of the zero imaginary part.
2511  return std::complex<RealType> (t, as<RealType> (0));
2512  }
2513  static std::complex<RealType> safeConvert (const unsigned long t) {
2514  // Let RealType handle the conversion of the zero imaginary part.
2515  return std::complex<RealType> (t, asSafe<RealType> (0));
2516  }
2517 };
2518 
2519 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
2520 
2522 template<class RealType>
2523 class ValueTypeConversionTraits<std::complex<RealType>, long long> {
2524 public:
2525  inline static std::complex<RealType> convert (const long long t) {
2526  // Let RealType handle the conversion of the zero imaginary part.
2527  return std::complex<RealType> (t, as<RealType> (0));
2528  }
2529  static std::complex<RealType> safeConvert (const long long t) {
2530  // Let RealType handle the conversion of the zero imaginary part.
2531  return std::complex<RealType> (t, asSafe<RealType> (0));
2532  }
2533 };
2534 
2536 template<class RealType>
2537 class ValueTypeConversionTraits<std::complex<RealType>, unsigned long long> {
2538 public:
2539  inline static std::complex<RealType> convert (const unsigned long long t) {
2540  // Let RealType handle the conversion of the zero imaginary part.
2541  return std::complex<RealType> (t, as<RealType> (0));
2542  }
2543  static std::complex<RealType> safeConvert (const unsigned long long t) {
2544  // Let RealType handle the conversion of the zero imaginary part.
2545  return std::complex<RealType> (t, asSafe<RealType> (0));
2546  }
2547 };
2548 
2549 #endif // HAVE_TEUCHOS_LONG_LONG_INT
2550 #endif // HAVE_TEUCHOS_COMPLEX
2551 
2552 //
2553 // * Conversions for dd_real and qd_real
2554 //
2555 
2556 #ifdef HAVE_TEUCHOS_QD
2557 
2559 template <>
2560 class ValueTypeConversionTraits<double, qd_real> {
2561 public:
2562  inline static double convert (const qd_real t) {
2563  return to_double (t);
2564  }
2565  static double safeConvert (const qd_real t) {
2566  // std::numeric_limits<double>::min() gives the minimum _positive_
2567  // normalized value of type double. IEEE 754 floating-point
2568  // values can change sign just by flipping the sign bit, so the
2569  // "most negative" finite double is just the negative of the "most
2570  // positive" finite double.
2571  const qd_real minVal = -std::numeric_limits<double>::max ();
2572  const qd_real maxVal = std::numeric_limits<double>::max ();
2573 
2575  t < minVal || t > maxVal,
2576  std::range_error,
2577  "Teuchos::ValueTypeConversionTraits<double, qd_real>::safeConvert: "
2578  "Input qd_real t = " << t << " is out of the valid range [" << minVal
2579  << ", " << maxVal << "] for conversion to double.");
2580 
2581  return to_double (t);
2582  }
2583 };
2584 
2586 template <>
2587 class ValueTypeConversionTraits<float, qd_real> {
2588 public:
2589  inline static float convert (const qd_real t) {
2590  // In a debug build, this should also test the double->float
2591  // conversion for overflow.
2592  return as<float> (to_double (t));
2593  }
2594 
2595  static float safeConvert (const qd_real t) {
2596  // std::numeric_limits<float>::min() gives the minimum _positive_
2597  // normalized value of type float. IEEE 754 floating-point
2598  // values can change sign just by flipping the sign bit, so the
2599  // "most negative" finite float is just the negative of the "most
2600  // positive" finite float.
2601  //
2602  // qd_real has a constructor for double, but not for float,
2603  // so we cast to double first.
2604  const qd_real minVal = static_cast<double> (-std::numeric_limits<float>::max ());
2605  const qd_real maxVal = static_cast<double> (std::numeric_limits<float>::max ());
2606 
2608  t < minVal || t > maxVal,
2609  std::range_error,
2610  "Teuchos::ValueTypeConversionTraits<float, qd_real>::safeConvert: "
2611  "Input qd_real t = " << t << " is out of the valid range [" << minVal
2612  << ", " << maxVal << "] for conversion to float.");
2613 
2614  // This should also test the double->float conversion for overflow.
2615  return asSafe<float> (to_double (t));
2616  }
2617 };
2618 
2620 template <>
2621 class ValueTypeConversionTraits<int, qd_real> {
2622 public:
2623  inline static int convert (const qd_real t) {
2624  return to_int (t);
2625  }
2626  static int safeConvert (const qd_real t) {
2627  // qd_real has a constructor for int.
2628  const qd_real minVal = std::numeric_limits<int>::min ();
2629  const qd_real maxVal = std::numeric_limits<int>::max ();
2630 
2632  t < minVal || t > maxVal,
2633  std::range_error,
2634  "Teuchos::ValueTypeConversionTraits<int, qd_real>::safeConvert: "
2635  "Input qd_real t = " << t << " is out of the valid range [" << minVal
2636  << ", " << maxVal << "] for conversion to int.");
2637  return to_int (t);
2638  }
2639 };
2640 
2642 template <>
2643 class ValueTypeConversionTraits<dd_real, qd_real> {
2644 public:
2645  inline static dd_real convert (const qd_real t) {
2646  return to_dd_real(t);
2647  }
2648  static dd_real safeConvert (const qd_real t) {
2649  // std::numeric_limits<dd_real>::min() gives the minimum
2650  // _positive_ (normalized? not sure what this means for dd_real --
2651  // mfh 14 Nov 2012) value of type dd_real. dd_real values are
2652  // built from two IEEE 754 doubles. This means they can change
2653  // sign just by flipping the sign bit, so the "most negative"
2654  // finite dd_real is just the negative of the "most positive"
2655  // finite dd_real.
2656  //
2657  // qd_real has a constructor for dd_real.
2658  const qd_real minVal = -std::numeric_limits<dd_real>::max ();
2659  const qd_real maxVal = std::numeric_limits<dd_real>::max ();
2660 
2662  t < minVal || t > maxVal,
2663  std::range_error,
2664  "Teuchos::ValueTypeConversionTraits<dd_real, qd_real>::safeConvert: "
2665  "Input qd_real t = " << t << " is out of the valid range [" << minVal
2666  << ", " << maxVal << "] for conversion to dd_real.");
2667 
2668  return to_dd_real (t);
2669  }
2670 };
2671 
2673 template <>
2674 class ValueTypeConversionTraits<double, dd_real> {
2675 public:
2676  inline static double convert (const dd_real t) {
2677  return to_double (t);
2678  }
2679  static double safeConvert (const dd_real t) {
2680  // std::numeric_limits<double>::min() gives the minimum _positive_
2681  // normalized value of type double. IEEE 754 floating-point
2682  // values can change sign just by flipping the sign bit, so the
2683  // "most negative" finite double is just the negative of the "most
2684  // positive" finite double.
2685  //
2686  // qd_real has a constructor for double.
2687  const dd_real minVal = -std::numeric_limits<double>::max ();
2688  const dd_real maxVal = std::numeric_limits<double>::max ();
2689 
2691  t < minVal || t > maxVal,
2692  std::range_error,
2693  "Teuchos::ValueTypeConversionTraits<double, dd_real>::safeConvert: "
2694  "Input dd_real t = " << t << " is out of the valid range [" << minVal
2695  << ", " << maxVal << "] for conversion to double.");
2696 
2697  return to_double (t);
2698  }
2699 };
2700 
2702 template <>
2703 class ValueTypeConversionTraits<float, dd_real> {
2704 public:
2705  inline static float convert (const dd_real t) {
2706  // This also checks for double->float overflow in a debug build.
2707  return as<float> (to_double (t));
2708  }
2709  static float safeConvert (const dd_real t) {
2710  // std::numeric_limits<float>::min() gives the minimum _positive_
2711  // normalized value of type float. IEEE 754 floating-point
2712  // values can change sign just by flipping the sign bit, so the
2713  // "most negative" finite float is just the negative of the "most
2714  // positive" finite float.
2715  //
2716  // dd_real has a constructor for double but not for float,
2717  // so we cast to double first.
2718  const dd_real minVal = static_cast<double> (-std::numeric_limits<float>::max ());
2719  const dd_real maxVal = static_cast<double> (std::numeric_limits<float>::max ());
2720 
2722  t < minVal || t > maxVal,
2723  std::range_error,
2724  "Teuchos::ValueTypeConversionTraits<float, dd_real>::safeConvert: "
2725  "Input dd_real t = " << t << " is out of the valid range [" << minVal
2726  << ", " << maxVal << "] for conversion to float.");
2727 
2728  // This also checks for double->float overflow.
2729  return as<float> (to_double (t));
2730  }
2731 };
2732 
2734 template <>
2735 class ValueTypeConversionTraits<int, dd_real> {
2736 public:
2737  inline static int convert (const dd_real t) {
2738  return to_int (t);
2739  }
2740  static int safeConvert (const dd_real t) {
2741  // dd_real has a constructor for int.
2742  const dd_real minVal = std::numeric_limits<int>::min ();
2743  const dd_real maxVal = std::numeric_limits<int>::max ();
2744 
2746  t < minVal || t > maxVal,
2747  std::range_error,
2748  "Teuchos::ValueTypeConversionTraits<int, dd_real>::safeConvert: "
2749  "Input dd_real t = " << t << " is out of the valid range [" << minVal
2750  << ", " << maxVal << "] for conversion to int.");
2751  return to_int (t);
2752  }
2753 };
2754 
2756 template <>
2757 class ValueTypeConversionTraits<qd_real, long unsigned int> {
2758 public:
2759  inline static qd_real convert( const long unsigned int t ) {
2760  // FIXME (mfh 14 Nov 2012): qd_real unfortunately lacks a
2761  // constructor or conversion function for conversions from
2762  // built-in integer types other than int. However, it does allow
2763  // reading in values from a string. We could use this to convert
2764  // from any type to qd_real, by first writing the value to an
2765  // std::ostringstream, then creating a qd_real from the resulting
2766  // string.
2767  return ValueTypeConversionTraits<qd_real,int>::convert(ValueTypeConversionTraits<int,long unsigned int>::convert(t));
2768  }
2769  inline static qd_real safeConvert( const long unsigned int t )
2770  { return ValueTypeConversionTraits<qd_real,int>::safeConvert(ValueTypeConversionTraits<int,long unsigned int>::safeConvert(t)); }
2771 };
2772 
2774 template <>
2775 class ValueTypeConversionTraits<dd_real, long unsigned int> {
2776 public:
2777  inline static dd_real convert( const long unsigned int t ) {
2778  // FIXME (mfh 14 Nov 2012): dd_real unfortunately lacks a
2779  // constructor or conversion function for conversions from
2780  // built-in integer types other than int. However, it does allow
2781  // reading in values from a string. We could use this to convert
2782  // from any type to dd_real, by first writing the value to an
2783  // std::ostringstream, then creating a dd_real from the resulting
2784  // string.
2785  return ValueTypeConversionTraits<dd_real,int>::convert(ValueTypeConversionTraits<int,long unsigned int>::convert(t));
2786  }
2787  inline static dd_real safeConvert( const long unsigned int t )
2788  { return ValueTypeConversionTraits<dd_real,int>::safeConvert(ValueTypeConversionTraits<int,long unsigned int>::safeConvert(t)); }
2789 };
2790 
2791 #endif // HAVE_TEUCHOS_QD
2792 
2793 // ToDo: Add more specializations as needed!
2794 
2795 
2796 } // end namespace Teuchos
2797 
2798 
2799 #endif // TEUCHOS_AS_HPP
static short convert(const int t)
Convert the given int to a short.
static unsigned int convert(const double t)
Convert the given double to an unsigned int.
static int safeConvert(const unsigned long t)
Convert from unsigned long to int, checking for overflow first.
static unsigned int convert(const float t)
Convert the given float to an unsigned int.
static unsigned long safeConvert(const float t)
Convert the given float to an unsigned long, checking first or under- or overflow.
TypeTo operator()(const TypeFrom &t)
Definition: Teuchos_as.hpp:387
static short safeConvert(const long t)
Convert from long to short, checking for overflow first.
static short safeConvert(const double t)
Convert the given double to a short, checking for overflow first.
static OutType safeConvert(const std::string &t)
Definition: Teuchos_as.hpp:617
static short safeConvert(const float t)
Convert the given float to a short, checking for overflow first.
static int safeConvert(const std::string &t)
Convert the given std::string to an int, with checks.
Definition: Teuchos_as.hpp:852
static long safeConvert(const std::string &t)
Convert the given std::string to a long, with checks.
Definition: Teuchos_as.hpp:785
static long convert(const float t)
Convert the given float to an long.
static unsigned short convert(const float t)
Convert the given float to an unsigned short.
TypeTo asSafe(const TypeFrom &t)
Convert from one value type to another, with validity checks if appropriate.
Definition: Teuchos_as.hpp:363
static unsigned int safeConvert(const std::string &t)
Convert the given std::string to an unsigned int, with checks.
Definition: Teuchos_as.hpp:897
static short convert(const std::string &t)
Convert the given std::string to a short.
Definition: Teuchos_as.hpp:947
static TypeTo safeConvert(const TypeFrom t)
Convert t from a TypeFrom object to a TypeTo object, with checks for validity.
Definition: Teuchos_as.hpp:189
static long double convert(const std::string &t)
Definition: Teuchos_as.hpp:678
static float safeConvert(const std::string &t)
Definition: Teuchos_as.hpp:662
static unsigned long safeConvert(const std::string &t)
Convert the given std::string to an unsigned long, with checks.
Definition: Teuchos_as.hpp:807
static unsigned long safeConvertToUnsignedLong(const std::string &t)
Convert the given std::string to an intermediate unsigned long, with checks.
Definition: Teuchos_as.hpp:870
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
static TypeTo convert(const TypeFrom t)
Convert t from a TypeFrom object to a TypeTo object.
Definition: Teuchos_as.hpp:182
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
static int convert(const std::string &t)
Convert the given std::string to an int.
Definition: Teuchos_as.hpp:857
static int convert(const float t)
Convert the given float to an int.
static long convert(const std::string &t)
Convert the given std::string to a long.
Definition: Teuchos_as.hpp:790
static long convert(const double t)
Convert the given double to long.
static unsigned long convert(const std::string &t)
Convert the given std::string to an unsigned long.
Definition: Teuchos_as.hpp:812
static int convert(const long t)
Convert the given long to an int.
static unsigned int convert(const unsigned long t)
Convert the given unsigned long to an unsigned int.
static long safeConvert(const double t)
Convert the given double to long, checking for overflow first.
static int safeConvert(const double t)
Convert the given double to an int, checking for overflow first.
static unsigned short safeConvert(const double t)
Convert the given double to an unsigned short, checking for overflow first.
static unsigned short safeConvert(const float t)
Convert the given float to an unsigned short, checking for overflow first.
static short convert(const double t)
Convert the given double to a short.
static long safeConvert(const unsigned int t)
Convert from unsigned int to long, checking for overflow first.
static int safeConvert(const float t)
Convert the given float to an int.
static unsigned int safeConvert(const float t)
Convert the given float to an unsigned int, checking first or under- or overflow. ...
static short convert(const float t)
Convert the given float to a short.
static unsigned int safeConvert(const double t)
Convert the given double to an unsigned int, checking for overflow first.
static unsigned short convert(const std::string &t)
Convert the given std::string to an unsigned short.
Definition: Teuchos_as.hpp:992
static long safeConvertToLong(const std::string &t)
Convert the given std::string to an intermediate long, with checks.
Definition: Teuchos_as.hpp:825
static long safeConvert(const float t)
Convert the given float to an long, checking first for overflow.
Function object wrapper for as().
Definition: Teuchos_as.hpp:382
static unsigned int safeConvert(const long t)
Convert from long to unsigned int, checking for underflow or overflow first.
static int convert(const double t)
Convert the given double to an int.
static unsigned long safeConvertToUnsignedLong(const std::string &t)
Convert the given std::string to an intermediate unsigned long, with checks.
Definition: Teuchos_as.hpp:960
static long double safeConvert(const std::string &t)
Definition: Teuchos_as.hpp:688
Default traits class for all conversions between value types.
Definition: Teuchos_as.hpp:179
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Definition: Teuchos_as.hpp:287
static unsigned int convert(const std::string &t)
Convert the given std::string to an unsigned int.
Definition: Teuchos_as.hpp:902
static long safeConvertToLong(const std::string &t)
Convert the given std::string to an intermediate long, with checks.
Definition: Teuchos_as.hpp:915
static short convert(const long t)
Convert the given long to a short.
static unsigned short safeConvert(const std::string &t)
Convert the given std::string to an unsigned short, with checks.
Definition: Teuchos_as.hpp:987
static double safeConvert(const std::string &t)
Definition: Teuchos_as.hpp:642
static unsigned int safeConvert(const unsigned long t)
Convert from unsigned long to unsigned int, checking for overflow first.
Default traits class that just returns typeid(T).name().
static int convert(const unsigned long t)
Convert the given unsigned long to an int.
static int safeConvert(const long t)
Convert from long to int, checking for overflow first.
static unsigned long convert(const double t)
Convert the given double to an unsigned long.
static long convert(const unsigned int t)
Convert the given unsigned int to a long.
static unsigned long safeConvert(const double t)
Convert the given double to an unsigned long, checking for overflow first.
static short safeConvert(const int t)
Convert from int to short, checking for overflow first.
static unsigned short convert(const double t)
Convert the given double to an unsigned short.
static short safeConvert(const std::string &t)
Convert the given std::string to a short, with checks.
Definition: Teuchos_as.hpp:942
static unsigned long convert(const float t)
Convert the given float to an unsigned long.
static unsigned int convert(const long t)
Convert the given long to an unsigned int.