9

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";
}
vitaut
  • 49,672
  • 25
  • 199
  • 336
Enderbyte09
  • 409
  • 1
  • 5
  • 11

5 Answers5

11

std::format was added to C++ in the C++20 standard. Unless you compile with C++20, you won't have std::format.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • See [this blog post](https://devblogs.microsoft.com/cppblog/msvc-cpp20-and-the-std-cpp20-switch/) – Chuck Walbourn Dec 03 '21 at 03:36
  • 2
    As stated in other answers, compiling with "ISO C++20 Standard (/std:c++20)" doesn't work in Visual Studio 2022. You need to set "Preview - Features from the Latest C++ Working Draft (/std:c++latest)". – steph643 Feb 24 '22 at 21:12
9

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.

Here is a quote:

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.

Jovibor
  • 759
  • 2
  • 6
  • 16
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

2

Format as a part of std is only implemented on clang++ and MSVC for now. So to make it work on other compilers such as g++, apple clang, use fmt which is the implementation of format.

Download the fmt package and place it in src folder along with your files and include it at compilation also specify that you are using c++ 20.

You can use it like this for reference:

g++ -I fmt/include -std=c++20 main.cpp
James Risner
  • 5,451
  • 11
  • 25
  • 47
0

Post here since this is the top result in google "error C2039: 'format_string': is not a member of 'std'". In case someone else having same issue.

So it's not exact same to original question. In my case, std::format is working perfectly in VS2022, but not std::format_string or std::wformat_string.

I have tried all suggestions and solutions online but then finally find out it's because in the old version MSVC (14.34.31933) the format header implement the feature with a different name:

template <class... _Args>
using _Fmt_string = _Basic_format_string<char, type_identity_t<_Args>...>;

But in latest version (14.36.32532), it's changed to format_string.

starball
  • 20,030
  • 7
  • 43
  • 238
catox
  • 1
  • 1