Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MacOS std::chrono::clock_cast not found

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
like image 558
Azyrod Avatar asked Mar 05 '26 22:03

Azyrod


2 Answers

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.

like image 142
Howard Hinnant Avatar answered Mar 07 '26 12:03

Howard Hinnant


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.
like image 38
Azyrod Avatar answered Mar 07 '26 11:03

Azyrod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!