'C++ namespace "std" has no member "format" despite #include <format>
I am new to C++. I am trying to store the current date and time as a string variable.
At this question, I found an answer, and installed the date.h
library.
However, when I try to use the code provided, I am met with the error:
namespace "std" has no member "format"
Despite having #include <format>
at the top of the script.
How can I fix this?
I am using Visual Studio 2022 on Windows 10, if that helps.
Here is my code:
#include <iostream>
#include <chrono>
#include <date.h>
#include <type_traits>
#include <format>
int main()
{
std::cout << "The current time is ";
auto start_time = std::format("{:%F %T}", std::chrono::system_clock::now());
static_assert(std::is_same_v<decltype(start_time), std::string>{});
std::cout << start_time << "\n";
}
Solution 1:[1]
std::format
was added to C++ in the C++20 standard. Unless you compile with C++20, you won't have std::format
.
Solution 2:[2]
As of december of 2021, the std::format
and some other C++20
facilities are available only under /std:c++latest
mode in Visual Studio 2019 and 2022.
As part of implementing C++20, there were some late discoveries which required changes to the ISO C++20 standard via the standard committee’s Defect Report (DR) process. This included Existing implementations (pre-DR) for these features are available under the /std:c++latest switch. We’re also tracking the DRs and are implementing those issue resolutions under /std:c++latest. Our plan is to make these capabilities available under the /std:c++20 switch after implementation of the full set of Standard Library DRs has completed.
When Microsoft finishes implementing all DRs, the std::format
will be available under the /std:c++20
switch.
Solution 3:[3]
For those running into this issue, using GCC or clang. The compiler only has partial support for all the new modules in c++20 https://en.cppreference.com/w/cpp/compiler_support/20
Solution 4:[4]
Use #include <algorithm>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | eerorika |
Solution 2 | Jovibor |
Solution 3 | Dave Mcpherson |
Solution 4 | Elvin Jafarov |