BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_streamed

Detailed Description

Provide a wrapper to format using an ostream operator<<

Outline

Purpose

Provide a wrapper to format using an ostream operator<<

Classes

Description

This component provides both a wrapper class template and a function template for creating a wrapper that enables bsl::formating values that offer an ostream insert operator<<. This wrapper is also compatible with std::format on platforms where it is provided.

bsl::format("Example: {}", bslfmt::streamed(ATypeWithoutAFormatter(42)));
Streamed< t_STREAMABLE > streamed(const t_STREAMABLE &object)

Using the bslfmt::streamed free function may be preferred to using the bslfmt::Streamed type directly because the bslfmt::streamed free-function supports simpler syntax on platforms that do not support class template argument deduction (CTAD, introduced in C++17).

For more (important) information please see the package documentation .

Usage

In this section we show the intended use of this component.

Formatting a Streamable Object

Suppose we want to format an object that already supports streaming into an ostream using the insert operator<<.

First, we define a type with a streaming operator but without a formatter specialization:

class NonFormattableType {};
std::ostream& operator<<(std::ostream& os, const NonFormattableType&)
{
return os << "The printout";
}
bsl::ostream & operator<<(bsl::ostream &stream, const bdlat_AttributeInfo &attributeInfo)

Then we create an instance of this type and use bsl::streamed to allow us to format it:

const NonFormattableType obj;

Next, we format the "value" using bsl::format with the wrapper-creator function:

bsl::string s = bsl::format("{}", bslfmt::streamed(obj));
Definition bslstl_string.h:1252

Finally, we verify the output is correct:

assert(s == "The printout");