fmtlib / fmtlib/fmt

Allow specializing formatter<std::wstring>

Closed
#4,946 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
25.8k
Forks
3.1k
Avg merge
3d 17h
Merged PRs (30d)
29

Description

We use `fmt` for our string formatting purposes. The quality is superb and we are super happy with the library.
We recently ran into an issue. We are forced to work with `std::wstring` in some places because Windows.
A watered down version of our logging facilities is:

```
template
void log(LogLevel level, fmt::format_string fmt_str, Args&&... args)
{
if (log_level_enough(level)) {
fmt::print("{}\n", fmt::format(fmt_str, std::forward(args)...));
}
}
```

This has an added value of being able to avoid preliminary formatting for log statements that don't match the current log level.
For example, we could pass a `const nlohmann::json&` as one of the arguments and only if `log_level_enough` returns true we'll call `json.dump()`. That allows for clean callsites and delayed work until absolutely necessary.
We define formatters for our types/external types (like `nlohmann::json`) to support this and the results are great.
We wanted to do the same for `std::wstring`. Unfortunately that is not possible due to two static asserts in `core.h`. Example hit: https://godbolt.org/z/q98eP5zGf

Doing as usually suggested:
```
log(LogLevel::trace, "this is formatted string {}", utf16_to_utf8(L"thing thing"));
```
would mean `utf16_to_utf8` logic is always invoked even if the current logging level would not print this log. Wrapping `std::wstring` in a custom type and creating a formatter for that is cumbersome and runs into lifetime extension questions that we would rather avoid.

Would it be possible to relax this restriction while forcing the user to provide their own formatter if they wish to perform this formatting?

Contributor guide

Open the contributing guide

Research direction

Start in core.h and reproduce the restriction using the linked Compiler Explorer example. Trace the two static assertions that reject std::wstring and inspect the existing formatter customization behavior. Done means user-provided formatters for std::wstring can be accepted while unsupported formatting remains rejected; add or update focused tests if the repository provides a relevant location.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.