Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to align multiple fields together in fmt?

Is it possible to align multiple fields together?

E.g.,

std::format("{:{}:<12} text", file, line)

to align as

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

main.cpp:1   text

and not as

main.cpp:   1 text

>Solution :

For the desired output, you could do:

std::cout << std::format("{0}:{1:<12} text", file, line) << '\n';

where:

  • {0} refers to the first argument (i.e. file)

  • {1:<12} is a nested field specifier for the second argument (i.e. line). The 1 refers to the second argument, and
    :<12 means left-align and pad to 12 characters

See live demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading