Skip to content

Cannot format a duration with unsigned (or user-defined) representation #96820

@dangelog

Description

@dangelog

Testcase: https://godbolt.org/z/EM7WxfEfT

#include <chrono>
#include <format>

int main()
{
    (void)
        std::format("{}", std::chrono::duration<unsigned>{});
}

Does not build:

In file included from /opt/compiler-explorer/clang-trunk-20240626/bin/../include/c++/v1/chrono:952:
/opt/compiler-explorer/clang-trunk-20240626/bin/../include/c++/v1/__chrono/formatter.h:594:63: error: no matching function for call to 'abs'
  594 |       __formatter::__format_chrono_using_chrono_specs(__sstr, chrono::abs(__value), __chrono_specs);
      |                                                               ^~~~~~~~~~~

chrono::abs is constrained for durations whose representation type is signed. This excludes unsigned types as well as any user-defined representation type.

Matching bug in libstdc++: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115668

Metadata

Metadata

Assignees

Labels

formatC++20 std::format or std::print, and anything related to themlibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions