From b23c8633fa4679830f5595bfb0ae33dc5d069666 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 5 Apr 2019 07:25:29 -0700 Subject: [PATCH] Detect presence of uintptr_t --- include/fmt/format.h | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index e13f498d..013416c9 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -28,10 +28,10 @@ #ifndef FMT_FORMAT_H_ #define FMT_FORMAT_H_ -#include #include #include #include +#include #include #include #include @@ -248,6 +248,12 @@ namespace internal { # define FMT_USE_GRISU 1 #endif +// A fallback implementation of uintptr_t for systems that lack it. +struct uintptr_t { + unsigned char value[sizeof(void*)]; +}; +typedef std::numeric_limits numutil; + template inline bool use_grisu() { return FMT_USE_GRISU && std::numeric_limits::is_iec559 && sizeof(T) <= sizeof(double); @@ -297,6 +303,20 @@ typename Allocator::value_type* allocate(Allocator& alloc, std::size_t n) { } // namespace internal FMT_END_NAMESPACE +namespace std { +using namespace fmt::internal; +// Standard permits specialization of std::numeric_limits. This specialization +// is used to detect presence of uintptr_t. +template <> +class numeric_limits + : public std::numeric_limits { + public: + static uintptr_t to_uint(const void* p) { + return fmt::internal::bit_cast(p); + } +}; +} // namespace std + FMT_BEGIN_NAMESPACE template class basic_writer; @@ -1392,7 +1412,7 @@ template class arg_formatter_base { format_specs specs = specs_ ? *specs_ : format_specs(); specs.flags = HASH_FLAG; specs.type = 'x'; - writer_.write_int(reinterpret_cast(p), specs); + writer_.write_int(internal::numutil::to_uint(p), specs); } protected: @@ -2764,7 +2784,7 @@ template class basic_writer { format_specs specs; specs.flags = HASH_FLAG; specs.type = 'x'; - write_int(reinterpret_cast(p), specs); + write_int(internal::numutil::to_uint(p), specs); } };