|
BDE 4.39.x Production Release
|
Provide a concept to check for the presence of a bsl::formatter.
Provide a concept to check for the presence of a bsl::formatter.
bsl::formatter presence conceptbsl::formatter is availableThis component conditionally provides bsl::formattable, a concept that determines if a type has a formatter enabled for a given character type (i.e. char or wchar_t). The concept is defined only if concepts are available on the current platform. The macro BSL_FORMATTABLE_DEFINED may be used to determine if the concept is present or not.
Because this component requires concepts support it is not available portably on all platforms, therefore any portable use of this concept should make use of the appropriate preprocessor guards.
A type is formattable if it can be used as a formatted argument of the bsl::format family of functions:
That in turn works if there is a matching bsl::formatter specialization for that type, such as:
In this section we show the intended use of this component.
Suppose we want to write different code depending on if a type has a formatter enabled, and fall back to using standard streaming if it does not.
First we create a type that supports streaming but not formatting with bsl::format:
Then we demonstrate that this type is not formattable, but an int is. Since the concept may not exists (older compilers/standards) we need to protect the code with the preprocessor:
Next we create a generic function to convert a value to string, center-aligned, and we provide two implementations, the first one for types that are formattable in case the concept exists, and just the stream-based variation if it does not.
Then, if the concept is present, we define the format-based overload:
Finally, we can call the centeredIn function and let concepts select the right variation (if concepts are available):