简体   繁体   English

尽管#include,C++ 命名空间“std”没有成员“格式”<format></format>

[英]C++ namespace "std" has no member "format" despite #include <format>

I am new to C++.我是 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.这个问题上,我找到了答案,并安装了date.h库。

However, when I try to use the code provided, I am met with the error:但是,当我尝试使用提供的代码时,我遇到了错误:

namespace "std" has no member "format"命名空间“std”没有成员“format”

Despite having #include <format> at the top of the script.尽管脚本顶部有#include <format>

How can I fix this?我怎样才能解决这个问题?

I am using Visual Studio 2022 on Windows 10, if that helps.如果有帮助,我正在 Windows 10 上使用 Visual Studio 2022。

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

std::format was added to C++ in the C++20 standard. std::format被添加到 C++20 标准中的 C++ 中。 Unless you compile with C++20, you won't have std::format .除非你用 C++20 编译,否则你不会有std::format

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 .截至 2021 年 12 月, std::format和其他一些C++20工具仅在Visual Studio 20192022中的/std:c++latest模式下可用。

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.作为实施 C++20 的一部分,有一些后期发现需要通过标准委员会的缺陷报告 (DR) 流程对 ISO C++20 标准进行更改。 This included Existing implementations (pre-DR) for these features are available under the /std:c++latest switch.这包括在 /std:c++latest 开关下提供了这些功能的现有实现(DR 之前)。 We're also tracking the DRs and are implementing those issue resolutions under /std:c++latest.我们还在跟踪 DR 并在 /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.我们的计划是在完成全套标准库 DR 的实施后,在 /std:c++20 开关下提供这些功能。

When Microsoft finishes implementing all DRs, the std::format will be available under the /std:c++20 switch.当 Microsoft 完成所有 DR 的实施后, std::format将在/std:c++20开关下可用。

For those running into this issue, using GCC or clang.对于遇到此问题的用户,请使用 GCC 或 clang。 The compiler only has partial support for all the new modules in c++20 https://en.cppreference.com/w/cpp/compiler_support/20编译器仅部分支持 c++20 https://en.cppreference.com/w/cpp/compiler_support/20中的所有新模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM