Home > Back-end >  C namespace "std" has no member "format" despite #include <format>
C namespace "std" has no member "format" despite #include <format>

Time:12-04

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";
}

CodePudding user response:

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

CodePudding user response:

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.

  • Related