1 // Formatting library for C++ - the core API
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
12 #include <cstdio> // std::FILE
16 #include <type_traits>
18 // The fmt library version in the form major * 10000 + minor * 100 + patch.
19 #define FMT_VERSION 50202
22 # define FMT_HAS_FEATURE(x) __has_feature(x)
24 # define FMT_HAS_FEATURE(x) 0
27 #if defined(__has_include) && !defined(__INTELLISENSE__) && \
28 !(defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1600)
29 # define FMT_HAS_INCLUDE(x) __has_include(x)
31 # define FMT_HAS_INCLUDE(x) 0
34 #ifdef __has_cpp_attribute
35 # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
37 # define FMT_HAS_CPP_ATTRIBUTE(x) 0
40 #if defined(__GNUC__) && !defined(__clang__)
41 # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
43 # define FMT_GCC_VERSION 0
46 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
47 # define FMT_HAS_GXX_CXX11 FMT_GCC_VERSION
49 # define FMT_HAS_GXX_CXX11 0
53 # define FMT_MSC_VER _MSC_VER
55 # define FMT_MSC_VER 0
58 // Check if relaxed C++14 constexpr is supported.
59 // GCC doesn't allow throw in constexpr until version 6 (bug 67371).
60 #ifndef FMT_USE_CONSTEXPR
61 # define FMT_USE_CONSTEXPR \
62 (FMT_HAS_FEATURE(cxx_relaxed_constexpr) || FMT_MSC_VER >= 1910 || \
63 (FMT_GCC_VERSION >= 600 && __cplusplus >= 201402L))
66 # define FMT_CONSTEXPR constexpr
67 # define FMT_CONSTEXPR_DECL constexpr
69 # define FMT_CONSTEXPR inline
70 # define FMT_CONSTEXPR_DECL
73 #ifndef FMT_USE_CONSTEXPR11
74 # define FMT_USE_CONSTEXPR11 \
75 (FMT_USE_CONSTEXPR || FMT_GCC_VERSION >= 406 || FMT_MSC_VER >= 1900)
77 #if FMT_USE_CONSTEXPR11
78 # define FMT_CONSTEXPR11 constexpr
80 # define FMT_CONSTEXPR11
84 # if FMT_HAS_FEATURE(cxx_override) || \
85 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
86 # define FMT_OVERRIDE override
92 #if FMT_HAS_FEATURE(cxx_explicit_conversions) || \
93 FMT_GCC_VERSION >= 405 || FMT_MSC_VER >= 1800
94 # define FMT_USE_EXPLICIT 1
95 # define FMT_EXPLICIT explicit
97 # define FMT_USE_EXPLICIT 0
102 # if FMT_HAS_FEATURE(cxx_nullptr) || \
103 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600
104 # define FMT_NULL nullptr
105 # define FMT_USE_NULLPTR 1
107 # define FMT_NULL NULL
110 #ifndef FMT_USE_NULLPTR
111 # define FMT_USE_NULLPTR 0
114 // Check if exceptions are disabled.
115 #ifndef FMT_EXCEPTIONS
116 # if (defined(__GNUC__) && !defined(__EXCEPTIONS)) || \
117 FMT_MSC_VER && !_HAS_EXCEPTIONS
118 # define FMT_EXCEPTIONS 0
120 # define FMT_EXCEPTIONS 1
124 // Define FMT_USE_NOEXCEPT to make fmt use noexcept (C++11 feature).
125 #ifndef FMT_USE_NOEXCEPT
126 # define FMT_USE_NOEXCEPT 0
129 #if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
130 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
131 # define FMT_DETECTED_NOEXCEPT noexcept
132 # define FMT_HAS_CXX11_NOEXCEPT 1
134 # define FMT_DETECTED_NOEXCEPT throw()
135 # define FMT_HAS_CXX11_NOEXCEPT 0
139 # if FMT_EXCEPTIONS || FMT_HAS_CXX11_NOEXCEPT
140 # define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT
142 # define FMT_NOEXCEPT
146 #ifndef FMT_BEGIN_NAMESPACE
147 # if FMT_HAS_FEATURE(cxx_inline_namespaces) || FMT_GCC_VERSION >= 404 || \
149 # define FMT_INLINE_NAMESPACE inline namespace
150 # define FMT_END_NAMESPACE }}
152 # define FMT_INLINE_NAMESPACE namespace
153 # define FMT_END_NAMESPACE } using namespace v5; }
155 # define FMT_BEGIN_NAMESPACE namespace fmt { FMT_INLINE_NAMESPACE v5 {
158 #if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
160 # define FMT_API __declspec(dllexport)
161 # elif defined(FMT_SHARED)
162 # define FMT_API __declspec(dllimport)
170 # define FMT_ASSERT(condition, message) assert((condition) && message)
173 // libc++ supports string_view in pre-c++17.
174 #if (FMT_HAS_INCLUDE(<string_view>) && \
175 (__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \
176 (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
177 # include <string_view>
178 # define FMT_STRING_VIEW std::basic_string_view
179 #elif FMT_HAS_INCLUDE(<experimental/string_view>) && __cplusplus >= 201402L
180 # include <experimental/string_view>
181 # define FMT_STRING_VIEW std::experimental::basic_string_view
184 // std::result_of is defined in <functional> in gcc 4.4.
185 #if FMT_GCC_VERSION && FMT_GCC_VERSION <= 404
186 # include <functional>
192 // An implementation of declval for pre-C++11 compilers such as gcc 4.
193 template <typename T
>
194 typename
std::add_rvalue_reference
<T
>::type
declval() FMT_NOEXCEPT
;
199 template <typename F
, typename
... Args
>
200 struct result_of
<F(Args
...)> {
201 // A workaround for gcc 4.4 that doesn't allow F to be a reference.
202 typedef typename
std::result_of
<
203 typename
std::remove_reference
<F
>::type(Args
...)>::type type
;
206 // Casts nonnegative integer to unsigned.
207 template <typename Int
>
208 FMT_CONSTEXPR typename
std::make_unsigned
<Int
>::type
to_unsigned(Int value
) {
209 FMT_ASSERT(value
>= 0, "negative value");
210 return static_cast<typename
std::make_unsigned
<Int
>::type
>(value
);
213 /** A contiguous memory buffer with an optional growing ability. */
214 template <typename T
>
217 basic_buffer(const basic_buffer
&) = delete;
218 void operator=(const basic_buffer
&) = delete;
222 std::size_t capacity_
;
225 // Don't initialize ptr_ since it is not accessed to save a few cycles.
226 basic_buffer(std::size_t sz
) FMT_NOEXCEPT
: size_(sz
), capacity_(sz
) {}
228 basic_buffer(T
*p
= FMT_NULL
, std::size_t sz
= 0, std::size_t cap
= 0)
229 FMT_NOEXCEPT
: ptr_(p
), size_(sz
), capacity_(cap
) {}
231 /** Sets the buffer data and capacity. */
232 void set(T
*buf_data
, std::size_t buf_capacity
) FMT_NOEXCEPT
{
234 capacity_
= buf_capacity
;
237 /** Increases the buffer capacity to hold at least *capacity* elements. */
238 virtual void grow(std::size_t capacity
) = 0;
241 typedef T value_type
;
242 typedef const T
&const_reference
;
244 virtual ~basic_buffer() {}
246 T
*begin() FMT_NOEXCEPT
{ return ptr_
; }
247 T
*end() FMT_NOEXCEPT
{ return ptr_
+ size_
; }
249 /** Returns the size of this buffer. */
250 std::size_t size() const FMT_NOEXCEPT
{ return size_
; }
252 /** Returns the capacity of this buffer. */
253 std::size_t capacity() const FMT_NOEXCEPT
{ return capacity_
; }
255 /** Returns a pointer to the buffer data. */
256 T
*data() FMT_NOEXCEPT
{ return ptr_
; }
258 /** Returns a pointer to the buffer data. */
259 const T
*data() const FMT_NOEXCEPT
{ return ptr_
; }
262 Resizes the buffer. If T is a POD type new elements may not be initialized.
264 void resize(std::size_t new_size
) {
269 /** Clears this buffer. */
270 void clear() { size_
= 0; }
272 /** Reserves space to store at least *capacity* elements. */
273 void reserve(std::size_t new_capacity
) {
274 if (new_capacity
> capacity_
)
278 void push_back(const T
&value
) {
280 ptr_
[size_
++] = value
;
283 /** Appends data to the end of the buffer. */
284 template <typename U
>
285 void append(const U
*begin
, const U
*end
);
287 T
&operator[](std::size_t index
) { return ptr_
[index
]; }
288 const T
&operator[](std::size_t index
) const { return ptr_
[index
]; }
291 typedef basic_buffer
<char> buffer
;
292 typedef basic_buffer
<wchar_t> wbuffer
;
294 // A container-backed buffer.
295 template <typename Container
>
296 class container_buffer
: public basic_buffer
<typename
Container::value_type
> {
298 Container
&container_
;
301 void grow(std::size_t capacity
) FMT_OVERRIDE
{
302 container_
.resize(capacity
);
303 this->set(&container_
[0], capacity
);
307 explicit container_buffer(Container
&c
)
308 : basic_buffer
<typename
Container::value_type
>(c
.size()), container_(c
) {}
311 // Extracts a reference to the container from back_insert_iterator.
312 template <typename Container
>
313 inline Container
&get_container(std::back_insert_iterator
<Container
> it
) {
314 typedef std::back_insert_iterator
<Container
> bi_iterator
;
315 struct accessor
: bi_iterator
{
316 accessor(bi_iterator iter
) : bi_iterator(iter
) {}
317 using bi_iterator::container
;
319 return *accessor(it
).container
;
322 struct error_handler
{
323 FMT_CONSTEXPR
error_handler() {}
324 FMT_CONSTEXPR
error_handler(const error_handler
&) {}
326 // This function is intentionally not constexpr to give a compile-time error.
327 FMT_API
void on_error(const char *message
);
330 template <typename T
>
331 struct no_formatter_error
: std::false_type
{};
332 } // namespace internal
334 #if FMT_GCC_VERSION && FMT_GCC_VERSION < 405
335 template <typename
... T
>
336 struct is_constructible
: std::false_type
{};
338 template <typename
... T
>
339 struct is_constructible
: std::is_constructible
<T
...> {};
343 An implementation of ``std::basic_string_view`` for pre-C++17. It provides a
344 subset of the API. ``fmt::basic_string_view`` is used for format strings even
345 if ``std::string_view`` is available to prevent issues when a library is
346 compiled with a different ``-std`` option than the client code (which is not
349 template <typename Char
>
350 class basic_string_view
{
356 typedef Char char_type
;
357 typedef const Char
*iterator
;
359 FMT_CONSTEXPR
basic_string_view() FMT_NOEXCEPT
: data_(FMT_NULL
), size_(0) {}
361 /** Constructs a string reference object from a C string and a size. */
362 FMT_CONSTEXPR
basic_string_view(const Char
*s
, size_t count
) FMT_NOEXCEPT
363 : data_(s
), size_(count
) {}
367 Constructs a string reference object from a C string computing
368 the size with ``std::char_traits<Char>::length``.
371 basic_string_view(const Char
*s
)
372 : data_(s
), size_(std::char_traits
<Char
>::length(s
)) {}
374 /** Constructs a string reference from a ``std::basic_string`` object. */
375 template <typename Alloc
>
376 FMT_CONSTEXPR
basic_string_view(
377 const std::basic_string
<Char
, Alloc
> &s
) FMT_NOEXCEPT
378 : data_(s
.data()), size_(s
.size()) {}
380 #ifdef FMT_STRING_VIEW
381 FMT_CONSTEXPR
basic_string_view(FMT_STRING_VIEW
<Char
> s
) FMT_NOEXCEPT
382 : data_(s
.data()), size_(s
.size()) {}
385 /** Returns a pointer to the string data. */
386 FMT_CONSTEXPR
const Char
*data() const { return data_
; }
388 /** Returns the string size. */
389 FMT_CONSTEXPR
size_t size() const { return size_
; }
391 FMT_CONSTEXPR iterator
begin() const { return data_
; }
392 FMT_CONSTEXPR iterator
end() const { return data_
+ size_
; }
394 FMT_CONSTEXPR
void remove_prefix(size_t n
) {
399 // Lexicographically compare this string reference to other.
400 int compare(basic_string_view other
) const {
401 size_t str_size
= size_
< other
.size_
? size_
: other
.size_
;
402 int result
= std::char_traits
<Char
>::compare(data_
, other
.data_
, str_size
);
404 result
= size_
== other
.size_
? 0 : (size_
< other
.size_
? -1 : 1);
408 friend bool operator==(basic_string_view lhs
, basic_string_view rhs
) {
409 return lhs
.compare(rhs
) == 0;
411 friend bool operator!=(basic_string_view lhs
, basic_string_view rhs
) {
412 return lhs
.compare(rhs
) != 0;
414 friend bool operator<(basic_string_view lhs
, basic_string_view rhs
) {
415 return lhs
.compare(rhs
) < 0;
417 friend bool operator<=(basic_string_view lhs
, basic_string_view rhs
) {
418 return lhs
.compare(rhs
) <= 0;
420 friend bool operator>(basic_string_view lhs
, basic_string_view rhs
) {
421 return lhs
.compare(rhs
) > 0;
423 friend bool operator>=(basic_string_view lhs
, basic_string_view rhs
) {
424 return lhs
.compare(rhs
) >= 0;
428 typedef basic_string_view
<char> string_view
;
429 typedef basic_string_view
<wchar_t> wstring_view
;
433 The function ``to_string_view`` adapts non-intrusively any kind of string or
434 string-like type if the user provides a (possibly templated) overload of
435 ``to_string_view`` which takes an instance of the string class
436 ``StringType<Char>`` and returns a ``fmt::basic_string_view<Char>``.
437 The conversion function must live in the very same namespace as
438 ``StringType<Char>`` to be picked up by ADL. Non-templated string types
439 like f.e. QString must return a ``basic_string_view`` with a fixed matching
445 inline string_view to_string_view(const my_string &s) {
446 return { s.data(), s.length() };
450 std::string message = fmt::format(my_string("The answer is {}"), 42);
453 template <typename Char
>
454 inline basic_string_view
<Char
>
455 to_string_view(basic_string_view
<Char
> s
) { return s
; }
457 template <typename Char
>
458 inline basic_string_view
<Char
>
459 to_string_view(const std::basic_string
<Char
> &s
) { return s
; }
461 template <typename Char
>
462 inline basic_string_view
<Char
> to_string_view(const Char
*s
) { return s
; }
464 #ifdef FMT_STRING_VIEW
465 template <typename Char
>
466 inline basic_string_view
<Char
>
467 to_string_view(FMT_STRING_VIEW
<Char
> s
) { return s
; }
470 // A base class for compile-time strings. It is defined in the fmt namespace to
471 // make formatting functions visible via ADL, e.g. format(fmt("{}"), 42).
472 struct compile_string
{};
474 template <typename S
>
475 struct is_compile_string
: std::is_base_of
<compile_string
, S
> {};
479 typename Enable
= typename
std::enable_if
<is_compile_string
<S
>::value
>::type
>
480 FMT_CONSTEXPR basic_string_view
<typename
S::char_type
>
481 to_string_view(const S
&s
) { return s
; }
483 template <typename Context
>
484 class basic_format_arg
;
486 template <typename Context
>
487 class basic_format_args
;
489 // A formatter for objects of type T.
490 template <typename T
, typename Char
= char, typename Enable
= void>
492 static_assert(internal::no_formatter_error
<T
>::value
,
493 "don't know how to format the type, include fmt/ostream.h if it provides "
494 "an operator<< that should be used");
496 // The following functions are not defined intentionally.
497 template <typename ParseContext
>
498 typename
ParseContext::iterator
parse(ParseContext
&);
499 template <typename FormatContext
>
500 auto format(const T
&val
, FormatContext
&ctx
) -> decltype(ctx
.out());
503 template <typename T
, typename Char
, typename Enable
= void>
504 struct convert_to_int
: std::integral_constant
<
505 bool, !std::is_arithmetic
<T
>::value
&& std::is_convertible
<T
, int>::value
> {};
509 struct dummy_string_view
{ typedef void char_type
; };
510 dummy_string_view
to_string_view(...);
511 using fmt::v5::to_string_view
;
513 // Specifies whether S is a string type convertible to fmt::basic_string_view.
514 template <typename S
>
515 struct is_string
: std::integral_constant
<bool, !std::is_same
<
516 dummy_string_view
, decltype(to_string_view(declval
<S
>()))>::value
> {};
518 template <typename S
>
520 typedef decltype(to_string_view(declval
<S
>())) result
;
521 typedef typename
result::char_type type
;
524 template <typename Char
>
525 struct named_arg_base
;
527 template <typename T
, typename Char
>
531 none_type
, named_arg_type
,
532 // Integer types should go first,
533 int_type
, uint_type
, long_long_type
, ulong_long_type
, bool_type
, char_type
,
534 last_integer_type
= char_type
,
535 // followed by floating-point types.
536 double_type
, long_double_type
, last_numeric_type
= long_double_type
,
537 cstring_type
, string_type
, pointer_type
, custom_type
540 FMT_CONSTEXPR
bool is_integral(type t
) {
541 FMT_ASSERT(t
!= internal::named_arg_type
, "invalid argument type");
542 return t
> internal::none_type
&& t
<= internal::last_integer_type
;
545 FMT_CONSTEXPR
bool is_arithmetic(type t
) {
546 FMT_ASSERT(t
!= internal::named_arg_type
, "invalid argument type");
547 return t
> internal::none_type
&& t
<= internal::last_numeric_type
;
550 template <typename Char
>
551 struct string_value
{
556 template <typename Context
>
557 struct custom_value
{
559 void (*format
)(const void *arg
, Context
&ctx
);
562 // A formatting argument value.
563 template <typename Context
>
566 typedef typename
Context::char_type char_type
;
571 long long long_long_value
;
572 unsigned long long ulong_long_value
;
574 long double long_double_value
;
576 string_value
<char_type
> string
;
577 string_value
<signed char> sstring
;
578 string_value
<unsigned char> ustring
;
579 custom_value
<Context
> custom
;
582 FMT_CONSTEXPR
value(int val
= 0) : int_value(val
) {}
583 value(unsigned val
) { uint_value
= val
; }
584 value(long long val
) { long_long_value
= val
; }
585 value(unsigned long long val
) { ulong_long_value
= val
; }
586 value(double val
) { double_value
= val
; }
587 value(long double val
) { long_double_value
= val
; }
588 value(const char_type
*val
) { string
.value
= val
; }
589 value(const signed char *val
) {
590 static_assert(std::is_same
<char, char_type
>::value
,
591 "incompatible string types");
594 value(const unsigned char *val
) {
595 static_assert(std::is_same
<char, char_type
>::value
,
596 "incompatible string types");
599 value(basic_string_view
<char_type
> val
) {
600 string
.value
= val
.data();
601 string
.size
= val
.size();
603 value(const void *val
) { pointer
= val
; }
605 template <typename T
>
606 explicit value(const T
&val
) {
608 custom
.format
= &format_custom_arg
<T
>;
611 const named_arg_base
<char_type
> &as_named_arg() {
612 return *static_cast<const named_arg_base
<char_type
>*>(pointer
);
616 // Formats an argument of a custom type, such as a user-defined class.
617 template <typename T
>
618 static void format_custom_arg(const void *arg
, Context
&ctx
) {
619 // Get the formatter type through the context to allow different contexts
620 // have different extension points, e.g. `formatter<T>` for `format` and
621 // `printf_formatter<T>` for `printf`.
622 typename
Context::template formatter_type
<T
>::type f
;
623 auto &&parse_ctx
= ctx
.parse_context();
624 parse_ctx
.advance_to(f
.parse(parse_ctx
));
625 ctx
.advance_to(f
.format(*static_cast<const T
*>(arg
), ctx
));
629 // Value initializer used to delay conversion to value and reduce memory churn.
630 template <typename Context
, typename T
, type TYPE
>
633 static const type type_tag
= TYPE
;
635 FMT_CONSTEXPR
init(const T
&v
) : val(v
) {}
636 FMT_CONSTEXPR
operator value
<Context
>() const { return value
<Context
>(val
); }
639 template <typename Context
, typename T
>
640 FMT_CONSTEXPR basic_format_arg
<Context
> make_arg(const T
&value
);
642 #define FMT_MAKE_VALUE(TAG, ArgType, ValueType) \
643 template <typename C> \
644 FMT_CONSTEXPR init<C, ValueType, TAG> make_value(ArgType val) { \
645 return static_cast<ValueType>(val); \
648 #define FMT_MAKE_VALUE_SAME(TAG, Type) \
649 template <typename C> \
650 FMT_CONSTEXPR init<C, Type, TAG> make_value(Type val) { return val; }
652 FMT_MAKE_VALUE(bool_type
, bool, int)
653 FMT_MAKE_VALUE(int_type
, short, int)
654 FMT_MAKE_VALUE(uint_type
, unsigned short, unsigned)
655 FMT_MAKE_VALUE_SAME(int_type
, int)
656 FMT_MAKE_VALUE_SAME(uint_type
, unsigned)
658 // To minimize the number of types we need to deal with, long is translated
659 // either to int or to long long depending on its size.
660 typedef std::conditional
<sizeof(long) == sizeof(int), int, long long>::type
663 (sizeof(long) == sizeof(int) ? int_type
: long_long_type
), long, long_type
)
664 typedef std::conditional
<sizeof(unsigned long) == sizeof(unsigned),
665 unsigned, unsigned long long>::type ulong_type
;
667 (sizeof(unsigned long) == sizeof(unsigned) ? uint_type
: ulong_long_type
),
668 unsigned long, ulong_type
)
670 FMT_MAKE_VALUE_SAME(long_long_type
, long long)
671 FMT_MAKE_VALUE_SAME(ulong_long_type
, unsigned long long)
672 FMT_MAKE_VALUE(int_type
, signed char, int)
673 FMT_MAKE_VALUE(uint_type
, unsigned char, unsigned)
675 // This doesn't use FMT_MAKE_VALUE because of ambiguity in gcc 4.4.
676 template <typename C
, typename Char
>
677 FMT_CONSTEXPR typename
std::enable_if
<
678 std::is_same
<typename
C::char_type
, Char
>::value
,
679 init
<C
, int, char_type
>>::type
make_value(Char val
) { return val
; }
681 template <typename C
>
682 FMT_CONSTEXPR typename
std::enable_if
<
683 !std::is_same
<typename
C::char_type
, char>::value
,
684 init
<C
, int, char_type
>>::type
make_value(char val
) { return val
; }
686 FMT_MAKE_VALUE(double_type
, float, double)
687 FMT_MAKE_VALUE_SAME(double_type
, double)
688 FMT_MAKE_VALUE_SAME(long_double_type
, long double)
690 // Formatting of wide strings into a narrow buffer and multibyte strings
691 // into a wide buffer is disallowed (https://github.com/fmtlib/fmt/pull/606).
692 FMT_MAKE_VALUE(cstring_type
, typename
C::char_type
*,
693 const typename
C::char_type
*)
694 FMT_MAKE_VALUE(cstring_type
, const typename
C::char_type
*,
695 const typename
C::char_type
*)
697 FMT_MAKE_VALUE(cstring_type
, signed char*, const signed char*)
698 FMT_MAKE_VALUE_SAME(cstring_type
, const signed char*)
699 FMT_MAKE_VALUE(cstring_type
, unsigned char*, const unsigned char*)
700 FMT_MAKE_VALUE_SAME(cstring_type
, const unsigned char*)
701 FMT_MAKE_VALUE_SAME(string_type
, basic_string_view
<typename
C::char_type
>)
702 FMT_MAKE_VALUE(string_type
,
703 typename basic_string_view
<typename
C::char_type
>::type
,
704 basic_string_view
<typename
C::char_type
>)
705 FMT_MAKE_VALUE(string_type
, const std::basic_string
<typename
C::char_type
>&,
706 basic_string_view
<typename
C::char_type
>)
707 FMT_MAKE_VALUE(pointer_type
, void*, const void*)
708 FMT_MAKE_VALUE_SAME(pointer_type
, const void*)
711 FMT_MAKE_VALUE(pointer_type
, std::nullptr_t
, const void*)
714 // Formatting of arbitrary pointers is disallowed. If you want to output a
715 // pointer cast it to "void *" or "const void *". In particular, this forbids
716 // formatting of "[const] volatile char *" which is printed as bool by
718 template <typename C
, typename T
>
719 typename
std::enable_if
<!std::is_same
<T
, typename
C::char_type
>::value
>::type
720 make_value(const T
*) {
721 static_assert(!sizeof(T
), "formatting of non-void pointers is disallowed");
724 template <typename C
, typename T
>
725 inline typename
std::enable_if
<
726 std::is_enum
<T
>::value
&& convert_to_int
<T
, typename
C::char_type
>::value
,
727 init
<C
, int, int_type
>>::type
728 make_value(const T
&val
) { return static_cast<int>(val
); }
730 template <typename C
, typename T
, typename Char
= typename
C::char_type
>
731 inline typename
std::enable_if
<
732 is_constructible
<basic_string_view
<Char
>, T
>::value
&&
733 !internal::is_string
<T
>::value
,
734 init
<C
, basic_string_view
<Char
>, string_type
>>::type
735 make_value(const T
&val
) { return basic_string_view
<Char
>(val
); }
737 template <typename C
, typename T
, typename Char
= typename
C::char_type
>
738 inline typename
std::enable_if
<
739 !convert_to_int
<T
, Char
>::value
&& !std::is_same
<T
, Char
>::value
&&
740 !std::is_convertible
<T
, basic_string_view
<Char
>>::value
&&
741 !is_constructible
<basic_string_view
<Char
>, T
>::value
&&
742 !internal::is_string
<T
>::value
,
743 // Implicit conversion to std::string is not handled here because it's
744 // unsafe: https://github.com/fmtlib/fmt/issues/729
745 init
<C
, const T
&, custom_type
>>::type
746 make_value(const T
&val
) { return val
; }
748 template <typename C
, typename T
>
749 init
<C
, const void*, named_arg_type
>
750 make_value(const named_arg
<T
, typename
C::char_type
> &val
) {
751 basic_format_arg
<C
> arg
= make_arg
<C
>(val
.value
);
752 std::memcpy(val
.data
, &arg
, sizeof(arg
));
753 return static_cast<const void*>(&val
);
756 template <typename C
, typename S
>
757 FMT_CONSTEXPR11 typename
std::enable_if
<
758 internal::is_string
<S
>::value
,
759 init
<C
, basic_string_view
<typename
C::char_type
>, string_type
>>::type
760 make_value(const S
&val
) {
761 // Handle adapted strings.
762 static_assert(std::is_same
<
763 typename
C::char_type
, typename
internal::char_t
<S
>::type
>::value
,
764 "mismatch between char-types of context and argument");
765 return to_string_view(val
);
768 // Maximum number of arguments with packed types.
769 enum { max_packed_args
= 15 };
770 enum : unsigned long long { is_unpacked_bit
= 1ull << 63 };
772 template <typename Context
>
774 } // namespace internal
776 // A formatting argument. It is a trivially copyable/constructible type to
777 // allow storage in basic_memory_buffer.
778 template <typename Context
>
779 class basic_format_arg
{
781 internal::value
<Context
> value_
;
782 internal::type type_
;
784 template <typename ContextType
, typename T
>
785 friend FMT_CONSTEXPR basic_format_arg
<ContextType
>
786 internal::make_arg(const T
&value
);
788 template <typename Visitor
, typename Ctx
>
789 friend FMT_CONSTEXPR typename
internal::result_of
<Visitor(int)>::type
790 visit_format_arg(Visitor
&&vis
, const basic_format_arg
<Ctx
> &arg
);
792 friend class basic_format_args
<Context
>;
793 friend class internal::arg_map
<Context
>;
795 typedef typename
Context::char_type char_type
;
800 explicit handle(internal::custom_value
<Context
> custom
): custom_(custom
) {}
802 void format(Context
&ctx
) const { custom_
.format(custom_
.value
, ctx
); }
805 internal::custom_value
<Context
> custom_
;
808 FMT_CONSTEXPR
basic_format_arg() : type_(internal::none_type
) {}
810 FMT_EXPLICIT
operator bool() const FMT_NOEXCEPT
{
811 return type_
!= internal::none_type
;
814 internal::type
type() const { return type_
; }
816 bool is_integral() const { return internal::is_integral(type_
); }
817 bool is_arithmetic() const { return internal::is_arithmetic(type_
); }
824 Visits an argument dispatching to the appropriate visit method based on
825 the argument type. For example, if the argument type is ``double`` then
826 ``vis(value)`` will be called with the value of type ``double``.
829 template <typename Visitor
, typename Context
>
830 FMT_CONSTEXPR typename
internal::result_of
<Visitor(int)>::type
831 visit_format_arg(Visitor
&&vis
, const basic_format_arg
<Context
> &arg
) {
832 typedef typename
Context::char_type char_type
;
834 case internal::none_type
:
836 case internal::named_arg_type
:
837 FMT_ASSERT(false, "invalid argument type");
839 case internal::int_type
:
840 return vis(arg
.value_
.int_value
);
841 case internal::uint_type
:
842 return vis(arg
.value_
.uint_value
);
843 case internal::long_long_type
:
844 return vis(arg
.value_
.long_long_value
);
845 case internal::ulong_long_type
:
846 return vis(arg
.value_
.ulong_long_value
);
847 case internal::bool_type
:
848 return vis(arg
.value_
.int_value
!= 0);
849 case internal::char_type
:
850 return vis(static_cast<char_type
>(arg
.value_
.int_value
));
851 case internal::double_type
:
852 return vis(arg
.value_
.double_value
);
853 case internal::long_double_type
:
854 return vis(arg
.value_
.long_double_value
);
855 case internal::cstring_type
:
856 return vis(arg
.value_
.string
.value
);
857 case internal::string_type
:
858 return vis(basic_string_view
<char_type
>(
859 arg
.value_
.string
.value
, arg
.value_
.string
.size
));
860 case internal::pointer_type
:
861 return vis(arg
.value_
.pointer
);
862 case internal::custom_type
:
863 return vis(typename basic_format_arg
<Context
>::handle(arg
.value_
.custom
));
865 return vis(monostate());
868 template <typename Visitor
, typename Context
>
869 FMT_CONSTEXPR typename
internal::result_of
<Visitor(int)>::type
870 visit(Visitor
&&vis
, const basic_format_arg
<Context
> &arg
) {
871 return visit_format_arg(std::forward
<Visitor
>(vis
), arg
);
874 // Parsing context consisting of a format string range being parsed and an
875 // argument counter for automatic indexing.
876 template <typename Char
, typename ErrorHandler
= internal::error_handler
>
877 class basic_parse_context
: private ErrorHandler
{
879 basic_string_view
<Char
> format_str_
;
883 typedef Char char_type
;
884 typedef typename basic_string_view
<Char
>::iterator iterator
;
886 explicit FMT_CONSTEXPR
basic_parse_context(
887 basic_string_view
<Char
> format_str
, ErrorHandler eh
= ErrorHandler())
888 : ErrorHandler(eh
), format_str_(format_str
), next_arg_id_(0) {}
890 // Returns an iterator to the beginning of the format string range being
892 FMT_CONSTEXPR iterator
begin() const FMT_NOEXCEPT
{
893 return format_str_
.begin();
896 // Returns an iterator past the end of the format string range being parsed.
897 FMT_CONSTEXPR iterator
end() const FMT_NOEXCEPT
{ return format_str_
.end(); }
899 // Advances the begin iterator to ``it``.
900 FMT_CONSTEXPR
void advance_to(iterator it
) {
901 format_str_
.remove_prefix(internal::to_unsigned(it
- begin()));
904 // Returns the next argument index.
905 FMT_CONSTEXPR
unsigned next_arg_id();
907 FMT_CONSTEXPR
bool check_arg_id(unsigned) {
908 if (next_arg_id_
> 0) {
909 on_error("cannot switch from automatic to manual argument indexing");
915 void check_arg_id(basic_string_view
<Char
>) {}
917 FMT_CONSTEXPR
void on_error(const char *message
) {
918 ErrorHandler::on_error(message
);
921 FMT_CONSTEXPR ErrorHandler
error_handler() const { return *this; }
924 typedef basic_parse_context
<char> format_parse_context
;
925 typedef basic_parse_context
<wchar_t> wformat_parse_context
;
928 typedef basic_parse_context
<char> parse_context
;
929 typedef basic_parse_context
<wchar_t> wparse_context
;
932 // A map from argument names to their values for named arguments.
933 template <typename Context
>
936 arg_map(const arg_map
&) = delete;
937 void operator=(const arg_map
&) = delete;
939 typedef typename
Context::char_type char_type
;
942 basic_string_view
<char_type
> name
;
943 basic_format_arg
<Context
> arg
;
949 void push_back(value
<Context
> val
) {
950 const internal::named_arg_base
<char_type
> &named
= val
.as_named_arg();
951 map_
[size_
] = entry
{named
.name
, named
.template deserialize
<Context
>()};
956 arg_map() : map_(FMT_NULL
), size_(0) {}
957 void init(const basic_format_args
<Context
> &args
);
958 ~arg_map() { delete [] map_
; }
960 basic_format_arg
<Context
> find(basic_string_view
<char_type
> name
) const {
961 // The list is unsorted, so just return the first matching name.
962 for (entry
*it
= map_
, *end
= map_
+ size_
; it
!= end
; ++it
) {
963 if (it
->name
== name
)
970 // A type-erased reference to an std::locale to avoid heavy <locale> include.
973 const void *locale_
; // A type-erased pointer to std::locale.
977 locale_ref() : locale_(FMT_NULL
) {}
979 template <typename Locale
>
980 explicit locale_ref(const Locale
&loc
);
982 template <typename Locale
>
986 template <typename OutputIt
, typename Context
, typename Char
>
989 typedef OutputIt iterator
;
992 basic_parse_context
<Char
> parse_context_
;
994 basic_format_args
<Context
> args_
;
998 typedef Char char_type
;
999 typedef basic_format_arg
<Context
> format_arg
;
1001 context_base(OutputIt out
, basic_string_view
<char_type
> format_str
,
1002 basic_format_args
<Context
> ctx_args
,
1003 locale_ref loc
= locale_ref())
1004 : parse_context_(format_str
), out_(out
), args_(ctx_args
), loc_(loc
) {}
1006 // Returns the argument with specified index.
1007 format_arg
do_get_arg(unsigned arg_id
) {
1008 format_arg arg
= args_
.get(arg_id
);
1010 parse_context_
.on_error("argument index out of range");
1014 // Checks if manual indexing is used and returns the argument with
1016 format_arg
get_arg(unsigned arg_id
) {
1017 return this->parse_context().check_arg_id(arg_id
) ?
1018 this->do_get_arg(arg_id
) : format_arg();
1022 basic_parse_context
<char_type
> &parse_context() { return parse_context_
; }
1023 basic_format_args
<Context
> args() const { return args_
; } // DEPRECATED!
1024 basic_format_arg
<Context
> arg(unsigned id
) const { return args_
.get(id
); }
1026 internal::error_handler
error_handler() {
1027 return parse_context_
.error_handler();
1030 void on_error(const char *message
) { parse_context_
.on_error(message
); }
1032 // Returns an iterator to the beginning of the output range.
1033 iterator
out() { return out_
; }
1034 iterator
begin() { return out_
; } // deprecated
1036 // Advances the begin iterator to ``it``.
1037 void advance_to(iterator it
) { out_
= it
; }
1039 locale_ref
locale() { return loc_
; }
1042 template <typename Context
, typename T
>
1044 typedef decltype(make_value
<Context
>(
1045 declval
<typename
std::decay
<T
>::type
&>())) value_type
;
1046 static const type value
= value_type::type_tag
;
1049 template <typename Context
>
1050 FMT_CONSTEXPR11
unsigned long long get_types() { return 0; }
1052 template <typename Context
, typename Arg
, typename
... Args
>
1053 FMT_CONSTEXPR11
unsigned long long get_types() {
1054 return get_type
<Context
, Arg
>::value
| (get_types
<Context
, Args
...>() << 4);
1057 template <typename Context
, typename T
>
1058 FMT_CONSTEXPR basic_format_arg
<Context
> make_arg(const T
&value
) {
1059 basic_format_arg
<Context
> arg
;
1060 arg
.type_
= get_type
<Context
, T
>::value
;
1061 arg
.value_
= make_value
<Context
>(value
);
1065 template <bool IS_PACKED
, typename Context
, typename T
>
1066 inline typename
std::enable_if
<IS_PACKED
, value
<Context
>>::type
1067 make_arg(const T
&value
) {
1068 return make_value
<Context
>(value
);
1071 template <bool IS_PACKED
, typename Context
, typename T
>
1072 inline typename
std::enable_if
<!IS_PACKED
, basic_format_arg
<Context
>>::type
1073 make_arg(const T
&value
) {
1074 return make_arg
<Context
>(value
);
1076 } // namespace internal
1078 // Formatting context.
1079 template <typename OutputIt
, typename Char
>
1080 class basic_format_context
:
1081 public internal::context_base
<
1082 OutputIt
, basic_format_context
<OutputIt
, Char
>, Char
> {
1084 /** The character type for the output. */
1085 typedef Char char_type
;
1087 // using formatter_type = formatter<T, char_type>;
1088 template <typename T
>
1089 struct formatter_type
{ typedef formatter
<T
, char_type
> type
; };
1092 internal::arg_map
<basic_format_context
> map_
;
1094 basic_format_context(const basic_format_context
&) = delete;
1095 void operator=(const basic_format_context
&) = delete;
1097 typedef internal::context_base
<OutputIt
, basic_format_context
, Char
> base
;
1098 typedef typename
base::format_arg format_arg
;
1099 using base::get_arg
;
1102 using typename
base::iterator
;
1105 Constructs a ``basic_format_context`` object. References to the arguments are
1106 stored in the object so make sure they have appropriate lifetimes.
1108 basic_format_context(OutputIt out
, basic_string_view
<char_type
> format_str
,
1109 basic_format_args
<basic_format_context
> ctx_args
,
1110 internal::locale_ref loc
= internal::locale_ref())
1111 : base(out
, format_str
, ctx_args
, loc
) {}
1113 format_arg
next_arg() {
1114 return this->do_get_arg(this->parse_context().next_arg_id());
1116 format_arg
get_arg(unsigned arg_id
) { return this->do_get_arg(arg_id
); }
1118 // Checks if manual indexing is used and returns the argument with the
1120 format_arg
get_arg(basic_string_view
<char_type
> name
);
1123 template <typename Char
>
1124 struct buffer_context
{
1125 typedef basic_format_context
<
1126 std::back_insert_iterator
<internal::basic_buffer
<Char
>>, Char
> type
;
1128 typedef buffer_context
<char>::type format_context
;
1129 typedef buffer_context
<wchar_t>::type wformat_context
;
1133 An array of references to arguments. It can be implicitly converted into
1134 `~fmt::basic_format_args` for passing into type-erased formatting functions
1135 such as `~fmt::vformat`.
1138 template <typename Context
, typename
...Args
>
1139 class format_arg_store
{
1141 static const size_t NUM_ARGS
= sizeof...(Args
);
1143 // Packed is a macro on MinGW so use IS_PACKED instead.
1144 static const bool IS_PACKED
= NUM_ARGS
< internal::max_packed_args
;
1146 typedef typename
std::conditional
<IS_PACKED
,
1147 internal::value
<Context
>, basic_format_arg
<Context
>>::type value_type
;
1149 // If the arguments are not packed, add one more element to mark the end.
1150 static const size_t DATA_SIZE
=
1151 NUM_ARGS
+ (IS_PACKED
&& NUM_ARGS
!= 0 ? 0 : 1);
1152 value_type data_
[DATA_SIZE
];
1154 friend class basic_format_args
<Context
>;
1156 static FMT_CONSTEXPR11
unsigned long long get_types() {
1158 internal::get_types
<Context
, Args
...>() :
1159 internal::is_unpacked_bit
| NUM_ARGS
;
1163 #if FMT_USE_CONSTEXPR11
1164 static FMT_CONSTEXPR11
unsigned long long TYPES
= get_types();
1166 static const unsigned long long TYPES
;
1169 #if (FMT_GCC_VERSION && FMT_GCC_VERSION <= 405) || \
1170 (FMT_MSC_VER && FMT_MSC_VER <= 1800)
1171 // Workaround array initialization issues in gcc <= 4.5 and MSVC <= 2013.
1172 format_arg_store(const Args
&... args
) {
1173 value_type init
[DATA_SIZE
] =
1174 {internal::make_arg
<IS_PACKED
, Context
>(args
)...};
1175 std::memcpy(data_
, init
, sizeof(init
));
1178 format_arg_store(const Args
&... args
)
1179 : data_
{internal::make_arg
<IS_PACKED
, Context
>(args
)...} {}
1183 #if !FMT_USE_CONSTEXPR11
1184 template <typename Context
, typename
...Args
>
1185 const unsigned long long format_arg_store
<Context
, Args
...>::TYPES
=
1191 Constructs an `~fmt::format_arg_store` object that contains references to
1192 arguments and can be implicitly converted to `~fmt::format_args`. `Context`
1193 can be omitted in which case it defaults to `~fmt::context`.
1196 template <typename Context
= format_context
, typename
...Args
>
1197 inline format_arg_store
<Context
, Args
...>
1198 make_format_args(const Args
&... args
) { return {args
...}; }
1200 /** Formatting arguments. */
1201 template <typename Context
>
1202 class basic_format_args
{
1204 typedef unsigned size_type
;
1205 typedef basic_format_arg
<Context
> format_arg
;
1208 // To reduce compiled code size per formatting function call, types of first
1209 // max_packed_args arguments are passed in the types_ field.
1210 unsigned long long types_
;
1212 // If the number of arguments is less than max_packed_args, the argument
1213 // values are stored in values_, otherwise they are stored in args_.
1214 // This is done to reduce compiled code size as storing larger objects
1215 // may require more code (at least on x86-64) even if the same amount of
1216 // data is actually copied to stack. It saves ~10% on the bloat test.
1217 const internal::value
<Context
> *values_
;
1218 const format_arg
*args_
;
1221 bool is_packed() const { return (types_
& internal::is_unpacked_bit
) == 0; }
1223 typename
internal::type
type(unsigned index
) const {
1224 unsigned shift
= index
* 4;
1225 return static_cast<typename
internal::type
>(
1226 (types_
& (0xfull
<< shift
)) >> shift
);
1229 friend class internal::arg_map
<Context
>;
1231 void set_data(const internal::value
<Context
> *values
) { values_
= values
; }
1232 void set_data(const format_arg
*args
) { args_
= args
; }
1234 format_arg
do_get(size_type index
) const {
1237 auto num_args
= max_size();
1238 if (index
< num_args
)
1242 if (index
> internal::max_packed_args
)
1244 arg
.type_
= type(index
);
1245 if (arg
.type_
== internal::none_type
)
1247 internal::value
<Context
> &val
= arg
.value_
;
1248 val
= values_
[index
];
1253 basic_format_args() : types_(0) {}
1257 Constructs a `basic_format_args` object from `~fmt::format_arg_store`.
1260 template <typename
... Args
>
1261 basic_format_args(const format_arg_store
<Context
, Args
...> &store
)
1262 : types_(static_cast<unsigned long long>(store
.TYPES
)) {
1263 set_data(store
.data_
);
1268 Constructs a `basic_format_args` object from a dynamic set of arguments.
1271 basic_format_args(const format_arg
*args
, size_type count
)
1272 : types_(internal::is_unpacked_bit
| count
) {
1276 /** Returns the argument at specified index. */
1277 format_arg
get(size_type index
) const {
1278 format_arg arg
= do_get(index
);
1279 if (arg
.type_
== internal::named_arg_type
)
1280 arg
= arg
.value_
.as_named_arg().template deserialize
<Context
>();
1284 size_type
max_size() const {
1285 unsigned long long max_packed
= internal::max_packed_args
;
1286 return static_cast<size_type
>(
1287 is_packed() ? max_packed
: types_
& ~internal::is_unpacked_bit
);
1291 /** An alias to ``basic_format_args<context>``. */
1292 // It is a separate type rather than a typedef to make symbols readable.
1293 struct format_args
: basic_format_args
<format_context
> {
1294 template <typename
...Args
>
1295 format_args(Args
&&... arg
)
1296 : basic_format_args
<format_context
>(std::forward
<Args
>(arg
)...) {}
1298 struct wformat_args
: basic_format_args
<wformat_context
> {
1299 template <typename
...Args
>
1300 wformat_args(Args
&&... arg
)
1301 : basic_format_args
<wformat_context
>(std::forward
<Args
>(arg
)...) {}
1304 #ifndef FMT_USE_ALIAS_TEMPLATES
1305 # define FMT_USE_ALIAS_TEMPLATES FMT_HAS_FEATURE(cxx_alias_templates)
1307 #if FMT_USE_ALIAS_TEMPLATES
1308 /** String's character type. */
1309 template <typename S
>
1310 using char_t
= typename
std::enable_if
<internal::is_string
<S
>::value
,
1311 typename
internal::char_t
<S
>::type
>::type
;
1312 #define FMT_CHAR(S) fmt::char_t<S>
1314 template <typename S
, typename T
>
1315 using enable_if_string_t
=
1316 typename
std::enable_if
<internal::is_string
<S
>::value
, T
>::type
;
1317 #define FMT_ENABLE_IF_STRING(S, T) enable_if_string_t<S, T>
1319 template <typename S
>
1320 struct char_t
: std::enable_if
<
1321 internal::is_string
<S
>::value
, typename
internal::char_t
<S
>::type
> {};
1322 #define FMT_CHAR(S) typename char_t<S>::type
1324 #define FMT_ENABLE_IF_STRING(S, T) \
1325 typename std::enable_if<internal::is_string<S>::value, T>::type
1328 namespace internal
{
1329 template <typename Char
>
1330 struct named_arg_base
{
1331 basic_string_view
<Char
> name
;
1333 // Serialized value<context>.
1335 sizeof(basic_format_arg
<typename buffer_context
<Char
>::type
>)];
1337 named_arg_base(basic_string_view
<Char
> nm
) : name(nm
) {}
1339 template <typename Context
>
1340 basic_format_arg
<Context
> deserialize() const {
1341 basic_format_arg
<Context
> arg
;
1342 std::memcpy(&arg
, data
, sizeof(basic_format_arg
<Context
>));
1347 template <typename T
, typename Char
>
1348 struct named_arg
: named_arg_base
<Char
> {
1351 named_arg(basic_string_view
<Char
> name
, const T
&val
)
1352 : named_arg_base
<Char
>(name
), value(val
) {}
1355 template <typename
... Args
, typename S
>
1356 inline typename
std::enable_if
<!is_compile_string
<S
>::value
>::type
1357 check_format_string(const S
&) {}
1358 template <typename
... Args
, typename S
>
1359 typename
std::enable_if
<is_compile_string
<S
>::value
>::type
1360 check_format_string(S
);
1362 template <typename S
, typename
... Args
>
1363 struct checked_args
: format_arg_store
<
1364 typename buffer_context
<FMT_CHAR(S
)>::type
, Args
...> {
1365 typedef typename buffer_context
<FMT_CHAR(S
)>::type context
;
1367 checked_args(const S
&format_str
, const Args
&... args
):
1368 format_arg_store
<context
, Args
...>(args
...) {
1369 internal::check_format_string
<Args
...>(format_str
);
1372 basic_format_args
<context
> operator*() const { return *this; }
1375 template <typename Char
>
1376 std::basic_string
<Char
> vformat(
1377 basic_string_view
<Char
> format_str
,
1378 basic_format_args
<typename buffer_context
<Char
>::type
> args
);
1380 template <typename Char
>
1381 typename buffer_context
<Char
>::type::iterator
vformat_to(
1382 internal::basic_buffer
<Char
> &buf
, basic_string_view
<Char
> format_str
,
1383 basic_format_args
<typename buffer_context
<Char
>::type
> args
);
1388 Returns a named argument to be used in a formatting function.
1392 fmt::print("Elapsed time: {s:.2f} seconds", fmt::arg("s", 1.23));
1395 template <typename T
>
1396 inline internal::named_arg
<T
, char> arg(string_view name
, const T
&arg
) {
1400 template <typename T
>
1401 inline internal::named_arg
<T
, wchar_t> arg(wstring_view name
, const T
&arg
) {
1405 // Disable nested named arguments, e.g. ``arg("a", arg("b", 42))``.
1406 template <typename S
, typename T
, typename Char
>
1407 void arg(S
, internal::named_arg
<T
, Char
>) = delete;
1409 template <typename Container
>
1410 struct is_contiguous
: std::false_type
{};
1412 template <typename Char
>
1413 struct is_contiguous
<std::basic_string
<Char
> >: std::true_type
{};
1415 template <typename Char
>
1416 struct is_contiguous
<internal::basic_buffer
<Char
> >: std::true_type
{};
1418 /** Formats a string and writes the output to ``out``. */
1419 template <typename Container
, typename S
>
1420 typename
std::enable_if
<
1421 is_contiguous
<Container
>::value
, std::back_insert_iterator
<Container
>>::type
1423 std::back_insert_iterator
<Container
> out
,
1424 const S
&format_str
,
1425 basic_format_args
<typename buffer_context
<FMT_CHAR(S
)>::type
> args
) {
1426 internal::container_buffer
<Container
> buf(internal::get_container(out
));
1427 internal::vformat_to(buf
, to_string_view(format_str
), args
);
1431 template <typename Container
, typename S
, typename
... Args
>
1432 inline typename
std::enable_if
<
1433 is_contiguous
<Container
>::value
&& internal::is_string
<S
>::value
,
1434 std::back_insert_iterator
<Container
>>::type
1435 format_to(std::back_insert_iterator
<Container
> out
, const S
&format_str
,
1436 const Args
&... args
) {
1437 internal::checked_args
<S
, Args
...> ca(format_str
, args
...);
1438 return vformat_to(out
, to_string_view(format_str
), *ca
);
1441 template <typename S
, typename Char
= FMT_CHAR(S
)>
1442 inline std::basic_string
<Char
> vformat(
1443 const S
&format_str
,
1444 basic_format_args
<typename buffer_context
<Char
>::type
> args
) {
1445 return internal::vformat(to_string_view(format_str
), args
);
1450 Formats arguments and returns the result as a string.
1454 #include <fmt/core.h>
1455 std::string message = fmt::format("The answer is {}", 42);
1458 template <typename S
, typename
... Args
>
1459 inline std::basic_string
<FMT_CHAR(S
)> format(
1460 const S
&format_str
, const Args
&... args
) {
1461 return internal::vformat(
1462 to_string_view(format_str
),
1463 *internal::checked_args
<S
, Args
...>(format_str
, args
...));
1466 FMT_API
void vprint(std::FILE *f
, string_view format_str
, format_args args
);
1467 FMT_API
void vprint(std::FILE *f
, wstring_view format_str
, wformat_args args
);
1471 Prints formatted data to the file *f*. For wide format strings,
1472 *f* should be in wide-oriented mode set via ``fwide(f, 1)`` or
1473 ``_setmode(_fileno(f), _O_U8TEXT)`` on Windows.
1477 fmt::print(stderr, "Don't {}!", "panic");
1480 template <typename S
, typename
... Args
>
1481 inline FMT_ENABLE_IF_STRING(S
, void)
1482 print(std::FILE *f
, const S
&format_str
, const Args
&... args
) {
1483 vprint(f
, to_string_view(format_str
),
1484 internal::checked_args
<S
, Args
...>(format_str
, args
...));
1487 FMT_API
void vprint(string_view format_str
, format_args args
);
1488 FMT_API
void vprint(wstring_view format_str
, wformat_args args
);
1492 Prints formatted data to ``stdout``.
1496 fmt::print("Elapsed time: {0:.2f} seconds", 1.23);
1499 template <typename S
, typename
... Args
>
1500 inline FMT_ENABLE_IF_STRING(S
, void)
1501 print(const S
&format_str
, const Args
&... args
) {
1502 vprint(to_string_view(format_str
),
1503 internal::checked_args
<S
, Args
...>(format_str
, args
...));
1507 #endif // FMT_CORE_H_