From: Mikko Rasa Date: Sat, 1 Dec 2012 09:27:45 +0000 (+0200) Subject: Make lexical_cast symmetric X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=42ff7c629b2aa7411963bdcc2259a06b1d1de6d3 Make lexical_cast symmetric It has bugged me for a long time that lexical_cast took only the type of the non-string side as a template argument. Fix it so that it always takes both target and source types. The source type can be deduced, so this only requires changes to invocations that cast things to strings. --- diff --git a/source/io/seekable.cpp b/source/io/seekable.cpp index a75cf33..35ab5a0 100644 --- a/source/io/seekable.cpp +++ b/source/io/seekable.cpp @@ -45,7 +45,7 @@ namespace Msp { namespace IO { bad_seek::bad_seek(SeekOffset offset, SeekType type): - runtime_error(type==S_BEG ? lexical_cast(offset) : + runtime_error(type==S_BEG ? lexical_cast(offset) : type==S_CUR ? format("CUR%+d", offset) : type==S_END ? format("END%+d", offset) : format("SeekType(%d)", type)) diff --git a/source/stringcodec/except.cpp b/source/stringcodec/except.cpp index 1e1393c..b4d07ec 100644 --- a/source/stringcodec/except.cpp +++ b/source/stringcodec/except.cpp @@ -22,7 +22,7 @@ string invalid_sequence::format_sequence(const string::const_iterator &begin, co { if(!result.empty()) result += ' '; - result += lexical_cast(static_cast(*i), Fmt().fill('0').width(2).hex().uppercase()); + result += lexical_cast(static_cast(*i), Fmt().fill('0').width(2).hex().uppercase()); } return result; } diff --git a/source/strings/format.h b/source/strings/format.h index 6d8f739..65d10df 100644 --- a/source/strings/format.h +++ b/source/strings/format.h @@ -24,7 +24,7 @@ public: template Formatter &operator()(const T &a) { - result += lexical_cast(a, get_conversion()); + result += lexical_cast(a, get_conversion()); advance(); return *this; } diff --git a/source/strings/lexicalcast.h b/source/strings/lexicalcast.h index b6cba3b..c6a17da 100644 --- a/source/strings/lexicalcast.h +++ b/source/strings/lexicalcast.h @@ -106,23 +106,51 @@ void operator>>(const LexicalConverter &c, T &v) throw lexical_error("conversion failure"); } -// The main interface to the lexical conversion machinery +// Helper struct to provide partial template specialization + +template +struct LexicalCast; template -inline T lexical_cast(const std::string &s, const Fmt &f = Fmt()) +struct LexicalCast { - LexicalConverter conv(s, f); - T result; - conv>>result; - return result; -} + static T cast(const std::string &s, const Fmt &f = Fmt()) + { + LexicalConverter conv(s, f); + T result; + conv>>result; + return result; + } +}; -template -inline std::string lexical_cast(const T &v, const Fmt &f = Fmt()) +template +struct LexicalCast +{ + static std::string cast(const F &v, const Fmt &f = Fmt()) + { + LexicalConverter conv(f); + conv< +struct LexicalCast +{ + static std::string cast(const std::string &v, const Fmt &f = Fmt()) + { + LexicalConverter conv(f); + conv< +inline T lexical_cast(const F &v, const Fmt &f = Fmt()) { - LexicalConverter conv(f); - conv<::cast(v, f); } } // namespace Msp diff --git a/tests/lexicalcast.cpp b/tests/lexicalcast.cpp index a983f1b..2a4a787 100644 --- a/tests/lexicalcast.cpp +++ b/tests/lexicalcast.cpp @@ -207,7 +207,7 @@ void LexicalCastTests::test_values(const Value *values) { for(const Value *i=values; i->format; ++i) { - string result = lexical_cast(i->value, i->format); + string result = lexical_cast(i->value, i->format); expect_equal(result, result==i->result, format("result == \"%s\"", c_escape(i->result))); } }