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?
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.
std
; otherwise, it would be undefined behavior –
Cleromancy std::format
! –
Chilblain 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.
std
; otherwise, it would be undefined behavior –
Cleromancy std::format
! –
Chilblain 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
.
© 2022 - 2024 — McMap. All rights reserved.
std::format
. – Spotlightstd::format
is, or would be, provided by the C++ standard library provided by your C++ implementation. Which is a piece of code. A piece of code that doesn't implementstd::format
. So you're asking how to make it implement something it doesn't implement. – Intercommunionstd::format
. – Intercommunion<format>
header - so I think you're out of luck. – FirepowerCString
has aFormat
member, and conversion betweenCString
andstd::string
(orstd::wstring
) is relatively trivial. – Firepower