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?
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?
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 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.
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
.