Fix #2817: add compile-time checking to ostream overloads of fmt::print

This commit is contained in:
timsong-cpp 2022-03-17 23:55:32 -05:00 committed by Victor Zverovich
parent 3d19be282a
commit 17ba99c1d2

View File

@ -11,6 +11,7 @@
#include <ostream> #include <ostream>
#include "format.h" #include "format.h"
#include "xchar.h"
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
@ -115,7 +116,8 @@ struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
FMT_MODULE_EXPORT FMT_MODULE_EXPORT
template <typename Char> template <typename Char>
void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str, void vprint(std::basic_ostream<Char>& os,
basic_string_view<type_identity_t<Char>> format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) { basic_format_args<buffer_context<type_identity_t<Char>>> args) {
auto buffer = basic_memory_buffer<Char>(); auto buffer = basic_memory_buffer<Char>();
detail::vformat_to(buffer, format_str, args); detail::vformat_to(buffer, format_str, args);
@ -132,12 +134,17 @@ void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
\endrst \endrst
*/ */
FMT_MODULE_EXPORT FMT_MODULE_EXPORT
template <typename S, typename... Args, template <typename... Args>
typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>> void print(std::ostream& os, format_string<Args...> fmt, Args&&... args) {
void print(std::basic_ostream<Char>& os, const S& format_str, Args&&... args) { vprint(os, fmt, fmt::make_format_args(args...));
vprint(os, to_string_view(format_str),
fmt::make_format_args<buffer_context<Char>>(args...));
} }
FMT_MODULE_EXPORT
template <typename... Args>
void print(std::wostream& os, wformat_string<Args...> fmt, Args&&... args) {
vprint(os, fmt, fmt::make_wformat_args(args...));
}
FMT_END_NAMESPACE FMT_END_NAMESPACE
#endif // FMT_OSTREAM_H_ #endif // FMT_OSTREAM_H_