Skip to content

std::chrono conversion does not compile with libc++ #675

@dbahrdt

Description

@dbahrdt

Describe the bug

I'm using jsoncons to convert std::chrono::seconds to/from json using clang and libc++.
I'm getting the following compiler error:

include/jsoncons/reflect/json_conv_traits.hpp:1636:54: error: cannot convert 'bigint' (aka 'basic_bigint<allocator<unsigned long>>') to 'long long' without a conversion operator
 1636 |                         return result_type(in_place, static_cast<Rep>(n));

The source is:

                    case semantic_tag::epoch_milli:
                    {
                        auto sv = j.as_string_view();
                        bigint n;
                        auto r = to_bigint(sv.data(), sv.length(), n);
                        if (!r) {return result_type(jsoncons::unexpect, conv_errc::not_epoch);}
                        if (n != 0)
                        {
                            n = n / millis_in_second;
                        }
                        return result_type(in_place, static_cast<Rep>(n));

In lib++ Rep is long long (https://github.com/llvm/llvm-project/blob/804c0345f5dc0fbb5bea5af6486ebdcfc0c99cb4/libcxx/include/chrono#L103).
jsoncons::basic_bigint defines cast operators for int64_t.
However, libc++ defines int64_t to be long.

The error can be fixed for this specific compiler/architecture/stdlib combination by adding a respective cast operator to bigint:

    explicit operator long long() const
    {
      return static_cast<long long>(static_cast<int64_t>(*this));
    }

Since other compilers may declare int64_t to be long long this is not a generic solution.
See https://godbolt.org/z/YvPza36YY for a minimum working example, albeit with a rather old jsoncons version.

What compiler, architecture, and operating system?

  • Compiler: clang version 21.1.6
  • Architecture: x86_64-pc-linux-gnu
  • Operating system: Arch Linux

What jsoncons library version?

1.5.0 via Conan

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions