3

I am trying to use the new C++20 chrono library, more specifically the new clock_cast.

If I compile on Linux with clang++ 16.0.6 it works as expected.
However, if I try to compile on MacOS I encounter the following error:

error: no member named 'clock_cast' in namespace 'std::chrono'

Clang is version 17.0.5 on the Mac, so I would expect the chrono library to be implemented, do I need to add another include?
I couldn't find anything online about this issue.

Clang version:

Homebrew clang version 17.0.5
Target: arm64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

Also tried with the default xcode clang version, same error.

Minimum reproducible example:

#include <chrono>

int main() {
    auto time = std::chrono::high_resolution_clock::now();
    auto system_time = std::chrono::clock_cast<std::chrono::system_clock>(time);
}

Compilation options:

/opt/homebrew/opt/llvm/bin/clang++ -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -D_LARGEFILE_SOURCE -isystem /opt/homebrew/Cellar/openssl@3/3.1.4/include -std=gnu++20 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk -fPIC -o test.cpp.o -c test.cpp
4
  • Add the flag -stdlib=libc++. Default lib might be conforming only one of the older C++ standards. Commented Nov 24, 2023 at 0:32
  • @273K Just tried - same result Commented Nov 24, 2023 at 0:35
  • Just noticed it's the homebrew llvm. I doubt you can find an answer here. Try Apple clang of xcode. Commented Nov 24, 2023 at 0:43
  • @273K I forgot to mention, but I also tried with the xcode clang, and i got the same error. Commented Nov 24, 2023 at 1:06

2 Answers 2

2

This has not yet been implemented.

Here is the libc++ status page for C++20:

https://libcxx.llvm.org/Status/Cxx20.html

The pertinent row is:

P0355R7 LWG Extending chrono to Calendars and Time Zones Jacksonville Partial [9]

The footnote is:

[9] P0355: The implementation status is:

  • Calendars mostly done in Clang 7
  • Input parsers not done
  • Stream output Obsolete due to P1361R2 “Integration of chrono with text formatting”
  • Time zone and leap seconds In Progress
  • TAI clock not done
  • GPS clock not done
  • UTC clock not done

Note that this:

auto time = std::chrono::high_resolution_clock::now();
auto system_time = std::chrono::clock_cast<std::chrono::system_clock>(time);

will never portably work as there is no defined relationship between high_resolution_clock and system_clock. On gcc these are the same clock and so clock_cast just performs the identity conversion. On libc++ high_resolution_clock is the same as steady_clock, and steady_clock has no defined relationship to system_clock.

clock_cast will perform conversions among: system_clock, utc_clock, tai_clock, gps_clock, file_clock, and any user-defined clock which defines one of these pairs of static member functions:

to_sys
from_sys

or

to_utc
from_utc

See this answer (section "Update for C++20") for an example of a user-defined clock which participates in the clock_cast infrastructure.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I see. Is there a way to use libstd++ with macos?
I'm sure there is, but I don't know how. Using my free, open-source C++20 chrono preview library is also an option. But there, it is date::clock_cast instead of std::chrono::clock_cast. And to get the time zone stuff to work, some installation is required.
2

I found a way to use libstd++ on MacOs, posting here in case it helps somebody.

brew install gcc # install gcc compiler with libstd++ support

# Need to use the newly installed binaries, note that g++ without 
# an additional version number will NOT work, as it is an alias to
# the default apple-clang compiler
export CC=gcc-13
export CXX=g++-13 # or check the output of `which g++-13` if you need the full path

# The version installed from brew at the time of writing is 13, 
# but could be a different version on your machine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.