|
BDE 4.39.x Production Release
|
Provide a suite of helper classes for printing bsl::variant.
Provide a suite of helper classes for printing bsl::variant.
bsl::variantbdlb::VariantPrinterThis component provides utility classes bdlb::VariantPrinter and bdlb::VariantPrinterUtil for printing bsl::variant.
The class bdlb::VariantPrinter can also be used with bsl::format (it defines the bslfmt::EnableStreamedFormatter trait). For information about the supported format options see the `bslfmt` package documentation .
This section illustrates intended use of this component.
In this example, we demonstrate how to use bdlb::VariantPrinterUtil to print bsl::variant to a stream.
First, we create a streamable type that throws int on copies. This will enable us to put a variant into a valueless state by copying one into it:
Next, we create a shorthand for VariantPrinterUtil and our variant type:
Then, we declare a variable of variant type, which can hold, among other types, a ThrowOnCopy:
Next, we start doing some output:
Then, we see that the type VariantPrinter<...> returned by makePrinter has a standard BDE-style print accessor that will control formatting and indenting:
Now, we assign a ThrowOnCopy to the variant, which will throw when copied, leaving mV in a valueless state, and we observe what happens when we then print it.
Finally, we see the output created by all this: