From 63b1f00eb87fd366209ca7e09300fd1e309eb7ee Mon Sep 17 00:00:00 2001 From: Paul Cornett Date: Mon, 27 Jan 2020 11:33:05 -0800 Subject: [PATCH] Allow wxStrto... functions to accept nullptr --- include/wx/wxcrt.h | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/include/wx/wxcrt.h b/include/wx/wxcrt.h index fc3bf0b681..cfdc47257e 100644 --- a/include/wx/wxcrt.h +++ b/include/wx/wxcrt.h @@ -844,18 +844,21 @@ template<> struct wxStrtoxCharType return NULL; } }; +#if __cplusplus >= 201103 +template<> struct wxStrtoxCharType +{ + typedef const char* Type; + static char** AsPointer(std::nullptr_t) + { + return nullptr; + } +}; +#endif template inline double wxStrtod(const wxString& nptr, T endptr) { - if ( endptr == 0 ) - { - // when we don't care about endptr, use the string representation that - // doesn't require any conversion (it doesn't matter for this function - // even if its UTF-8): - return wxStrtod(nptr.wx_str(), (wxStringCharType**)NULL); - } - else + if (endptr) { // note that it is important to use c_str() here and not mb_str() or // wc_str(), because we store the pointer into (possibly converted) @@ -864,6 +867,11 @@ inline double wxStrtod(const wxString& nptr, T endptr) return wxStrtod((CharType)nptr.c_str(), wxStrtoxCharType::AsPointer(endptr)); } + // when we don't care about endptr, use the string representation that + // doesn't require any conversion (it doesn't matter for this function + // even if its UTF-8): + wxStringCharType** p = NULL; + return wxStrtod(nptr.wx_str(), p); } template inline double wxStrtod(const wxCStrData& nptr, T endptr) @@ -882,15 +890,15 @@ inline double wxStrtod(const wxCStrData& nptr, T endptr) template \ inline rettype name(const wxString& nptr, T endptr, int base) \ { \ - if ( endptr == 0 ) \ - return name(nptr.wx_str(), (wxStringCharType**)NULL, base); \ - else \ + if (endptr) \ { \ typedef typename wxStrtoxCharType::Type CharType; \ return name((CharType)nptr.c_str(), \ wxStrtoxCharType::AsPointer(endptr), \ base); \ } \ + wxStringCharType** p = NULL; \ + return name(nptr.wx_str(), p, base); \ } \ template \ inline rettype name(const wxCStrData& nptr, T endptr, int base) \