Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++20 support in Visual Studio

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?

like image 717
Jonathan Wood Avatar asked May 02 '20 14:05

Jonathan Wood


People also ask

Is C supported in Visual Studio?

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 .

What version of C does Visual Studio use?

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.

Is C++20 fully supported?

Visual Studio 2019 (latest version 16.10, on Windows) is the only IDE currently coming with a compiler that fully supports C++20.

What version of C does Visual Studio 2019 use?

Available under /std:c++17 and /std:c++latest (or /std:c++20 starting in Visual Studio 2019 version 16.11):


3 Answers

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.

like image 134
S.S. Anne Avatar answered Oct 09 '22 02:10

S.S. Anne


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;
}
like image 39
PeterT Avatar answered Oct 09 '22 04:10

PeterT


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.

like image 3
Zailon Xwadastet Avatar answered Oct 09 '22 04:10

Zailon Xwadastet