Fmt: default formatting of standard objects -- use of <<

Created on 5 May 2020  路  3Comments  路  Source: fmtlib/fmt

I just tried to print std::this_thread::get_id() using {}. This doesn't compile. The error message under either c++ or llvm is not great (just refers to "type T").

  1. Is it ultimately intended that objects of any type explicitly described in the standard must be printable with {}? By "explicitly described" I mean things like class thread::id but not auto or a lambda function.

In particular class thread::id has a << method already -- should format fall back on that until the opposite is true?

FWIW no special constraints on thread::ids can be assumed except they must function as container keys. They my not be integers or pointers.

I'd try to make a pull request but I'm not even sure what ought to happen here except that {} should work with thread ids.

Most helpful comment

#include <fmt/ostream.h>

All 3 comments

#include <fmt/ostream.h>

As @agmt commented, {fmt} supports formatting of types with ostream operator<< via fmt/ostream.h: https://fmt.dev/latest/api.html#ostream-api

Thanks to you both and my apologies for missing that in the documentation.

Was this page helpful?
0 / 5 - 0 ratings