I'm wanting to use std::format
but Visual Studio says the std
namespace has no member format
.
It appears this is new for C++20. Is there a way to make it available?
Visual Studio Code is a lightweight, cross-platform development environment that runs on Windows, Mac, and Linux systems. The Microsoft C/C++ for Visual Studio Code extension supports IntelliSense, debugging, code formatting, auto-completion. Visual Studio for Mac doesn't support Microsoft C++, but does support .
In versions of Visual Studio 2019 before version 16.11, /std:c++latest is required to enable all the compiler and standard library features of C++20. For a list of supported language and library features, see What's New for C++ in Visual Studio.
Visual Studio 2019 (latest version 16.10, on Windows) is the only IDE currently coming with a compiler that fully supports C++20.
Available under /std:c++17 and /std:c++latest (or /std:c++20 starting in Visual Studio 2019 version 16.11):
As of the time of writing, no C++ standard library implements std::format
.
There are various implementations available on the web, like https://github.com/fmtlib/fmt (presumably the original source of the proposal, in fmt::
) and https://github.com/mknejp/std-format (which puts everything in std::experimental::
).
I wouldn't recommend pulling these into std
. If I had to deal with something like this, the solution I would go for would be:
Add a #define <some-unique-name>_format <wherever>::format
and then use <some-unique-name>_format
.
Then, once you get std::format
support, search-and-replace <some-unique-name>_format
with std::format
and toss the #define
.
It uses macros, but in the long run it's better than having unqualified format
everywhere.
You can use fmt as a sort of polyfill. It's not identical but has a significant feature overlap. So if you're careful about how you use it you can swap it out for <format>
once support is there.
#include <string>
#include <version>
#ifndef __cpp_lib_format
#include <fmt/core.h>
using fmt::format;
#else
#include <format>
using std::format;
#endif
int main()
{
std::string a = format("test {}",43);
return 0;
}
As of the release of Visual Studio 2019 version 16.10 (released May 25th 2021) support was added for std::format
. Just compile with /std:c++latest
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With