Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #ifndef CATA_COMPATIBILITY_H
  2. #define CATA_COMPATIBILITY_H
  3.  
  4. //--------------------------------------------------------------------------------------------------
  5. // HACK:
  6. // std::to_string is broken on MinGW (as of 13/01/2015), but is fixed in MinGW-w64 gcc 4.8.
  7. // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015
  8. // This is a minimal workaround pending the issue begin solved (if ever).
  9. // For proper linking these must be declared inline here, or moved to a cpp file.
  10. //--------------------------------------------------------------------------------------------------
  11. #include <string>
  12.  
  13. #define CATA_GCC_VER (__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + (__GNUC_PATCHLEVEL__)
  14.  
  15. #if defined(__MINGW32__) && !defined(__MINGW64__)
  16. # define CATA_NO_CPP11_STRING_CONVERSIONS
  17. #elif defined(__MINGW64__) && (CATA_GCC_VER < 40800)
  18. # define CATA_NO_CPP11_STRING_CONVERSIONS
  19. #endif
  20.  
  21. #if defined(CATA_NO_CPP11_STRING_CONVERSIONS)
  22. #include <cstdio>
  23. #include <limits>
  24.  
  25. inline std::string to_string(long const n)
  26. {
  27. //- and \0
  28. constexpr int size = std::numeric_limits<long>::digits10 + 2;
  29. char buffer[size];
  30. snprintf(buffer, size, "%ld", n);
  31. return buffer;
  32. }
  33.  
  34. inline std::string to_string(int const n)
  35. {
  36. //- and \0
  37. constexpr int size = std::numeric_limits<int>::digits10 + 2;
  38. char buffer[size];
  39. snprintf(buffer, size, "%d", n);
  40. return buffer;
  41. }
  42.  
  43. inline std::string to_string(double const n)
  44. {
  45. //- . \0 + snprintf default precision.
  46. constexpr int size = std::numeric_limits<double>::max_exponent10 + 6 + 3;
  47. char buffer[size];
  48. snprintf(buffer, size, "%f", n);
  49. return buffer;
  50. }
  51. #else //all other platforms
  52. #include <type_traits>
  53.  
  54. //mirrors the valid overloads of std::to_string
  55. template <typename T, typename std::enable_if<std::is_arithmetic<T>::value &&
  56. !std::is_same<T, bool>::value && !std::is_same<T, wchar_t>::value &&
  57. !std::is_same<T, char>::value && !std::is_same<T, char16_t>::value &&
  58. !std::is_same<T, char32_t>::value>::type* = nullptr>
  59. std::string to_string(T const n)
  60. {
  61. return to_string(n);
  62. }
  63.  
  64. #endif //CATA_NO_CPP11_STRING_CONVERSIONS
  65.  
  66. #endif //CATA_COMPATIBILITY_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement