|
BDE 4.39.x Production Release
|
Provide a base template for formatter specializations.
Provide a base template for formatter specializations.
bsl_format.h
This component provides a base template of the C++20 Standard Library's formatter, which is a customization point for user defined types seeking to use the formatting library.
It also provides a mechanism, when the standard library <format> header is available, to forward those partial specializations to the std namespace to enable use of std::format as well as bsl::format.
This header is not intended to be included directly. Please include <bsl_format.h> to be able to use specializations of bsl::formatter.
User-provided formatters are supported by the BSL implementation, just as they are by the standard library implementation. However, in order for them to be compatible with both implementations, there are specific requirements, notably:
T, should be declared in the same component header in which this type is declared to avoid issues due to users forgetting to include the header for the formatter.bsl, not std.parse function should be constexpr in C++20, but this is not required (and may not be possible) for earlier C++ standards.This section illustrates intended use of this component.
Suppose we have a custom type representing a date. And we want to output it to the stream in different formats depending on the circumstances using bsl::format function. The following example shows how this can be done.
First, we define our Date class:
Now, we define formatter specialization for the Date and in particular parse() and format() functions which will be called by bsl::format. Note that specialization must be defined in the namespace bsl.
Unfortunately, due to the position of this component in the class hierarchy, a full-fledged example would require duplicating a huge amount of code. A complete example of a custom formatter implementation can be found in the bslfmt_format component.