C++20 support in Visual Studio
Asked Answered
S

3

9

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?

Spotlight answered 2/5, 2020 at 14:51 Comment(16)
Are you asking if you can make a piece of code implement something that it doesn't implement?Intercommunion
which version are you using?Peres
@NicolBolas: Huh? I want to use std::format.Spotlight
@idclev463035818: Version of what? I'm using Visual Studio 16.5.1.Spotlight
@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.Intercommunion
Have you set your project options to use the 'latest' (preview) C++ standard?Firepower
@NicolBolas: No, that's not what I'm asking.Spotlight
@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?Spotlight
@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.Intercommunion
@JonathanWood: Since you're not getting the hint, I'll spell it out for you: Visual Studio (nor any other C++ standard library implementation ) does not yet support std::format.Intercommunion
@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.Spotlight
@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 <format> header - so I think you're out of luck.Firepower
@JonathanWood Nope, sorry. std::format is not implemented yet. Actually, no standard library implements it.Cleromancy
fyi Microsoft C++ language conformance table - learn.microsoft.com/en-us/cpp/overview/…Inna
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.Firepower
@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.Spotlight
C
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.

Cleromancy answered 2/5, 2020 at 15:3 Comment(2)
@TemplateRex What would you suggest doing? A namespace alias can't pull into std; otherwise, it would be undefined behaviorCleromancy
Visual Studio 2019 v16.10 has just added std::format!Chilblain
N
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;
}
Neurogenic answered 2/5, 2020 at 15:2 Comment(0)
C
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.

Cleromancy answered 2/5, 2020 at 15:3 Comment(2)
@TemplateRex What would you suggest doing? A namespace alias can't pull into std; otherwise, it would be undefined behaviorCleromancy
Visual Studio 2019 v16.10 has just added std::format!Chilblain
S
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.

Seafaring answered 1/10, 2021 at 23:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.