fmt icon indicating copy to clipboard operation
fmt copied to clipboard

Multiple {} for one object?

Open nuuSolutions opened this issue 1 year ago • 1 comments

println( "{}{}{}", 1, 2, 3 );                        // valid
println( "{}", std::tuple(1, 'b', 1.0/3.0) );        // valid
println( "{.2f}", std::tuple(1, 'b', 1.0/3.0) );     // error

// what I'd like and what an average user would expect to work
println( "{} {} {.2f}", std::tuple(1, 'b', 1.0/3.0) );

Question 1: can I let my own tuple-like type consume multiple {}s Question 2: why is std::tuple only taking one {}

nuuSolutions avatar Aug 29 '24 12:08 nuuSolutions

I'm familiar writing formatters for my types, but I fail to see how I could consume more than one {}. Maybe impossible by design. Maybe I just need a hint.

nuuSolutions avatar Aug 29 '24 12:08 nuuSolutions

Tuples are not destructured automatically but you could write your own tuple wrapper and provide a formatter that extracts individual elements, something like:

auto t = std::tuple(1, 'b');
fmt::print("{0:[0]} {0:[1]}", mytuple(t));

vitaut avatar Aug 29 '24 14:08 vitaut

I still wonder why the design choice was single {} for one tuple rather than n {}s

nuuSolutions avatar Aug 29 '24 18:08 nuuSolutions

{fmt} is modeled after Python's format which made this design choice. But even putting Python aside, autodestructuring would be extremely confusing with multiple arguments.

vitaut avatar Aug 29 '24 18:08 vitaut