8

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?

vitaut
  • 49,672
  • 25
  • 199
  • 336
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    Are you asking if you can *make* a piece of code implement something that it doesn't implement? – Nicol Bolas May 02 '20 at 14:53
  • which version are you using? – 463035818_is_not_an_ai May 02 '20 at 14:53
  • 1
    @NicolBolas: Huh? I want to use `std::format`. – Jonathan Wood May 02 '20 at 14:54
  • @idclev463035818: Version of what? I'm using Visual Studio 16.5.1. – Jonathan Wood May 02 '20 at 14:55
  • @JonathanWood: Yes, and `std::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 implement `std::format`. So you're asking how to make it implement something it doesn't implement. – Nicol Bolas May 02 '20 at 14:55
  • Have you set your project options to use the 'latest' (preview) C++ standard? – Adrian Mole May 02 '20 at 14:55
  • @NicolBolas: No, that's not what I'm asking. – Jonathan Wood May 02 '20 at 14:56
  • @AdrianMole: I'm not sure how to do that, but am not sure I want to use preview code. Are you saying Visual Studio does not yet have full support for C++20? – Jonathan Wood May 02 '20 at 14:57
  • @JonathanWood: But that's what your question boils down to, whether you recognize it or not. If your car doesn't support flying, you can't *make it* support flying. – Nicol Bolas May 02 '20 at 14:57
  • @JonathanWood: Since you're not getting the hint, I'll spell it out for you: Visual Studio (nor [any other C++ standard library implementation](https://en.cppreference.com/w/cpp/compiler_support) ) does not yet support `std::format`. – Nicol Bolas May 02 '20 at 15:00
  • @S.S.Anne: Is there an option setting somewhere for that? I couldn't find it. Also, when you say the latest version, do you mean later than 16.5.1? Looks like 16.5.4 is the latest. – Jonathan Wood May 02 '20 at 15:00
  • 5
    @JonathanWood In project properties -> C/C++ -> Language ... select "Use Latest (Preview) Draft Standard" However, I just tried (VS 16.5.4) and it doesn't have the `` header - so I think you're out of luck. – Adrian Mole May 02 '20 at 15:01
  • 1
    @JonathanWood Nope, sorry. std::format is not implemented yet. Actually, no standard library implements it. – S.S. Anne May 02 '20 at 15:01
  • fyi Microsoft C++ language conformance table - https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019 – Richard Critten May 02 '20 at 15:11
  • As you're using Visual Studio: the MFC/ATL `CString` has a `Format` member, and conversion between `CString` and `std::string` (or `std::wstring`) is relatively trivial. – Adrian Mole May 02 '20 at 15:12
  • @AdrianMole: I'm an old-time MFC programmer so this was my first thought. I feel like I should be using newer technologies but it's often much easier to stick with what works. – Jonathan Wood May 02 '20 at 15:16

3 Answers3

5

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;
}
PeterT
  • 7,981
  • 1
  • 26
  • 34
5

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.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
3

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.