Skip to content

How to handle dynamic format specifiers in custom formatters ? #1636

Closed
@BenjaminNavarro

Description

I am currently implementing an fmt-based formatter for the Eigen library since the original one uses ostreams and requires to build a formatter object to change the formatting.

The initial implementation is working and the first tests gives a 4x performance boost, which is great. However I am struggling with one issue and I can't find in the docs how to solve it.

Since my format string can be quite long (lot of parameters) I'd like to be able to construct it once and then pass it as an additional argument to the format function, similar to what is possible for floating point numbers precision (fmt::format("{:.{}f}", 3.14, 1);)

In my case I'd like to do:

constexpr auto heavy_format_str = "p{f};csep{, };rsep{;\n};rpre{[};rsuf{]};mpre{[};msuf{]}";
fmt::print("{:{}}", mat, heavy_format_str);

But I don't understand how to make it works. Do I have to do something specific in my formatter parse function to tell fmt that there is a {} that need to be formatted or do I have to, somehow, access myself the next argument and handle it myself?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions