mpusz / mp-units

The quantities and units library for C++
https://mpusz.github.io/mp-units/
MIT License
994 stars 79 forks source link

formatting/printing vector of quantities results in compile time error #543

Closed WBurzynski closed 5 months ago

WBurzynski commented 5 months ago

Not sure if It's this library bug or fmt, but trying to print vector of quantity results in compile time error.

Problem reproduced in compiler explorer:

https://godbolt.org/z/7v5jjh9Gs

Problematic code:

#include <mp-units/systems/si/si.h>
#include <vector>
#include "fmt/ranges.h"
#include "mp-units/format.h"
#include "fmt/format.h"

using namespace mp_units::si::unit_symbols;

int main()
{
    using Time = mp_units::quantity<mp_units::isq::time[mp_units::si::second]>;
    Time time = 1 * s;
    fmt::println("{}",time);
    std::vector<Time> vec{time,time};
    fmt::println("{}",vec);
}
mpusz commented 5 months ago

@WBurzynski, thanks for reporting! This seems to be related to #503. Initially, the fmtlib did not require the format() member function to be const, and this was how they implemented their own formatters. However, during the standardization process, this member function became const, and fmtlib updated the support in the next releases. mp-units stayed behind.

I actually started to work on this issue during the weekend, and hopefully, it will be fixed soon.

WBurzynski commented 5 months ago

Some time ago there was this issue in fmt: https://github.com/fmtlib/fmt/issues/2754 where const qualifiers were added to some functions. I suspect that either somewhere in fmt another const is missing or in auto format(const quantity& q, FormatContext& ctx) const quantity is too restrictive.

mpusz commented 5 months ago

No, the problem is that the member function format() should be const qualified, but it isn't. It was changed while fmt was standardized.