diff --git a/doc/api.rst b/doc/api.rst index 8a226758..01dd1493 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -200,6 +200,8 @@ The following user-defined literals are defined in ``fmt/format.h``. Utilities --------- +.. doxygenclass:: fmt::is_char + .. doxygentypedef:: fmt::char_t .. doxygenfunction:: fmt::formatted_size(string_view, const Args&...) diff --git a/include/fmt/color.h b/include/fmt/color.h index bc534805..f1683a71 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -518,13 +518,6 @@ inline void reset_color(basic_memory_buffer& buffer) FMT_NOEXCEPT { buffer.append(begin, end); } -// The following specialization disables using std::FILE as a character type, -// which is needed because or else -// fmt::print(stderr, fmt::emphasis::bold, ""); -// would take stderr (a std::FILE *) as the format string. -template <> struct is_string : std::false_type {}; -template <> struct is_string : std::false_type {}; - template std::basic_string vformat(const text_style& ts, basic_string_view format_str, diff --git a/include/fmt/core.h b/include/fmt/core.h index 7e22c166..baee5ea9 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -442,33 +442,25 @@ template class basic_string_view { using string_view = basic_string_view; using wstring_view = basic_string_view; +/** Specifies if ``T`` is a character type. Can be specialized by users. */ +template struct is_char : std::is_integral {}; + /** \rst - The function ``to_string_view`` adapts non-intrusively any kind of string or - string-like type if the user provides a (possibly templated) overload of - ``to_string_view`` which takes an instance of the string class - ``StringType`` and returns a ``fmt::basic_string_view``. - The conversion function must live in the very same namespace as - ``StringType`` to be picked up by ADL. Non-templated string types - like f.e. QString must return a ``basic_string_view`` with a fixed matching - char type. + Returns a string view of `s`. In order to add custom string type support to + {fmt} provide an overload of `to_string_view` for it in the same namespace as + the type for the argument-dependent lookup to work. **Example**:: namespace my_ns { - inline string_view to_string_view(const my_string &s) { + inline string_view to_string_view(const my_string& s) { return {s.data(), s.length()}; } } - std::string message = fmt::format(my_string("The answer is {}"), 42); \endrst */ -template -inline basic_string_view to_string_view(basic_string_view s) { - return s; -} - template inline basic_string_view to_string_view( const std::basic_string& s) { @@ -476,6 +468,11 @@ inline basic_string_view to_string_view( } template +inline basic_string_view to_string_view(basic_string_view s) { + return s; +} + +template ::value)> inline basic_string_view to_string_view(const Char* s) { return s; } @@ -597,10 +594,6 @@ struct is_string !std::is_same()))>::value> {}; -// Forward declare FILE* specialization defined in color.h -template <> struct is_string; -template <> struct is_string; - template struct char_t_impl { typedef decltype(to_string_view(std::declval())) result; typedef typename result::char_type type;