Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended method for adding formatter for third-party custom type #669

Closed
mikecrowe opened this issue Mar 4, 2018 · 2 comments
Closed

Comments

@mikecrowe
Copy link
Contributor

I'd like to add a formatter for std::chrono::duration in the style of #392 . This is straightforward to do:

template <typename Rep, typename Period>void format_arg(fmt::BasicFormatter<char> &f,                                                                                                                                             
                const char *&format_str, const std::chrono::duration<Rep, Period> &ms) {
    f.writer().write("{}s", std::chrono::duration<double>(ms).count());
}

But, in order for argument-dependent lookup to work, this function needs to either be in the fmt or std::chrono namespaces. I don't believe that I'm supposed to put it in the std namespace, so that leaves putting it in fmt. Is that acceptable, or is there a better way?

Thanks.

Mike.

@vitaut
Copy link
Contributor

vitaut commented Mar 4, 2018

Is that acceptable, or is there a better way?

Defining extension functions in the fmt namespace is perfectly acceptable.

Note, however, that the extension API will change in the next major version to accommodate constexpr format string checks and output iterators per standards committee feedback. The API is still WIP, but will likely look as follows: http://fmtlib.net/dev/api.html#formatting-user-defined-types. The 4.x branch will remain supported for compatibility with older compilers so migration to the new API is optional.

@vitaut vitaut closed this as completed Mar 4, 2018
@mikecrowe
Copy link
Contributor Author

Thanks for the quick answer!

Mike.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants