I can successfully compile this code on Windows with Visual Studio set for C++20 code generation:
c++20.cpp
:
#include <chrono>
int main()
{
const std::chrono::time_zone *timeZone = std::chrono::current_zone();
return 0;
}
On the Raspberry Pi 4 i have:
pi@rasp:~ $ g++ --version
g++ (Raspbian 10.2.1-6+rpi1) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
When compiling the previous code on the Raspberry with g++ I get:
pi@rasp:~ $ g++ -std=c++20 c++20.cpp
c++20.cpp: In function ‘int main()’:
c++20.cpp:5:21: error: ‘time_zone’ in namespace ‘std::chrono’ does not name a type; did you mean ‘time_point’?
5 | const std::chrono::time_zone *timeZone = std::chrono::current_zone();
| ^~~~~~~~~
| time_point
Isn't C++ versions standardized? Is it really possible that some functions can be missing from one operating system installation while being present on another?
C++ is standardized, but especially for recent standards, not every compiler supports every feature yet.
First of all, check the C++20 compiler support page to see if the feature you want is supported. In particular, the support for your wanted feature is:
Feature | GCC libstdc++ | MSVC STL |
---|---|---|
Calendar and time zone | 11 (partial)* 13 (partial)* 14 |
19.29 (16.10)* |
To get full support, you need at least GCC 14. As commenter @HolyBlackCat has pointed out, your program also compiles with the partial support from GCC 13. You're using GCC 10, which has no support at all. The easiest solution is to update to a more recent compiler.
If this is somehow not an option, you could use the corresponding feature testing macro:
#if __cpp_lib_chrono >= 201907L
// if you don't always need the feature, you could use it conditionally
#else
// alternatively, print an error if the feature is missing
#error "This program requires C++20 calendar and time zone support"
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With