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<string>(offset) :
type==S_CUR ? format("CUR%+d", offset) :
type==S_END ? format("END%+d", offset) :
format("SeekType(%d)", type))
throw lexical_error("conversion failure");
}
-// The main interface to the lexical conversion machinery
+// Helper struct to provide partial template specialization
+
+template<typename T, typename F>
+struct LexicalCast;
template<typename T>
-inline T lexical_cast(const std::string &s, const Fmt &f = Fmt())
+struct LexicalCast<T, std::string>
{
- 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<typename T>
-inline std::string lexical_cast(const T &v, const Fmt &f = Fmt())
+template<typename F>
+struct LexicalCast<std::string, F>
+{
+ static std::string cast(const F &v, const Fmt &f = Fmt())
+ {
+ LexicalConverter conv(f);
+ conv<<v;
+ return conv.get();
+ }
+};
+
+template<>
+struct LexicalCast<std::string, std::string>
+{
+ static std::string cast(const std::string &v, const Fmt &f = Fmt())
+ {
+ LexicalConverter conv(f);
+ conv<<v;
+ return conv.get();
+ }
+};
+
+// The main interface to the lexical conversion machinery
+
+template<typename T, typename F>
+inline T lexical_cast(const F &v, const Fmt &f = Fmt())
{
- LexicalConverter conv(f);
- conv<<v;
- return conv.get();
+ return LexicalCast<T, F>::cast(v, f);
}
} // namespace Msp
{
for(const Value<T> *i=values; i->format; ++i)
{
- string result = lexical_cast(i->value, i->format);
+ string result = lexical_cast<string>(i->value, i->format);
expect_equal(result, result==i->result, format("result == \"%s\"", c_escape(i->result)));
}
}