can't include std::format
Asked Answered
S

1

11

I have seen how useful std::format can be. But every time I try to include it on C++20 it does not work. it is apparently already included in its libraries but it does not appear, and there is no information online. Even cppreference has its example but it does not even run on its online compiler. I attached a snippet from it.

Does anyone of you guys know how to make it work without a super complex importing libraries from GitHub. I mainly work on VisualStudio.

#include <iostream>
#include <format>

int main() {
    std::cout << std::format("Hello {}!\n", "world");
}
main.cpp:2:10: fatal error: No such file or directory
    2 | #include <format>
      |          ^~~~~~~~
compilation terminated.
Syneresis answered 21/7, 2020 at 15:17 Comment(3)
format has not yet been implemented by any C++ standard library. So you'll have to download and install a stand-alone version for the time being.Fencing
@NicolBolas Thank you very much for your reply. That makes sense. Much appreciated.Syneresis
Even in gcc 11.0.2 with -std=c++20 this does not seem to exist yet.Celik
S
12

As of July 2020 none of the standard library implementations provide std::format. Until they do you can use the {fmt} library std::format is based on:

#include <iostream>
#include <fmt/core.h>

int main() {
  std::cout << fmt::format("Hello {}!\n", "world");
}

godbolt

Savory answered 27/7, 2020 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.