OsmanZakir-9751 avatar image
0 Votes"
OsmanZakir-9751 asked RLWA32-6355 commented

Using std::format in VS2019; gives error saying I need to use /std:c++latest

Hi, everyone at the MSDN Forums.

My issue is as I said in the title: Whenever I try to use std::format in my code when using the /std:c++20 compiler switch, it gives me a compiler error saying that std::format requires /std:c++latest with Concepts support. I read on the Microsoft site that support for std::format under /std:c++20 is already complete in version 16.11. Mine is 16.11.5. So why do I have this error?
Here's one piece of code I tried:

 // Osman Zakir
 // 10 / 25 / 2021
 // Beginning C++20: From Novice to Professional
 // Chapter 2 Exercises 4 and 5
 // Exercise 4 Specs:
  * your body mass index (BMI) is your weight, w, in kilograms divided by the square
  * of your height, h, in meters ( w/(h*h) ). Write a program to calculate the BMI from a weight
  * entered in pounds and a height entered in feet and inches. A kilogram is 2.2 pounds, and a foot
  * is 0.3048 meters.
 // Exercise 5 Specs:
  * Knowing your BMI with a precision higher than one decimal digit after the
  * decimal point is, well, pointless. Adjust the program of exercise 2-4 accordingly.
 import <iostream>;
 import <format>;
 import <cmath>;
 int main()
  std::cout << "Enter weight in pounds and height in feet and inches: ";
  double w{};
  double h{};
  std::cin >> w >> h;
  std::cin.ignore(32767, '\n');
  constexpr double lbs_to_kg{ 2.2046 };
  constexpr double ft_to_meters{ 0.3048 };
  const double w_kg{ w / lbs_to_kg };
  const double h_meters{ h * ft_to_meters };
  const double bmi{ w_kg / std::pow(h_meters, 2) };
  std::cout << std::format("Your BMI is {:.1f}kg/m2\n", bmi);

Compiler Command Line:

/JMC /permissive- /MP /ifcOutput "Debug\" /GS /analyze- /W3 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"Debug\vc142.pdb" /Zc:inline /fp:precise /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /std:c++latest /FC /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\chapter2ex4.pch" /diagnostics:column

Any help is appreciated. Thanks.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @OsmanZakir-9751,

Could you please share the document you are reading? C++20 added a new text formatting facility. Please read this document carefully. The document states

To start using <format> you just need to be using Visual Studio 2019 version 16.10 or later and you need to be compiling with / std:c++latest.

Regarding your question, you could find the answer here.

0 Votes 0 ·

I was looking at this page. I may have misunderstood something, though. Sorry about that.

And those StackOverflow answers are old now. since VS2019 supports std::format since version 16.10 was released.

0 Votes 0 ·

Hi @OsmanZakir-9751,

It is inevitable that you will encounter problems during the learning process. I look forward to your coming to the Q&A platform to ask questions again.

0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

Read this document In particular, refer to note 20abi which pertains to <format>

20abi Because of ongoing post-release work on the C++20 standard, <format>, the formatting parts of <chrono> (which rely on <format>), and the range factories and range adaptors from <ranges> (everything that needs the view concept) are only available under /std:c++latest. We'll make these features available under /std:c++20 after reaching agreement with WG21 that no further ABI-breaking changes are necessary. The remaining parts of <chrono> and the algorithms that apply to ranges are enabled under the /std:c++20 compiler option starting in Visual Studio 2019 version 16.11.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. That's helpful.

Where did my other comments go, like the where I said it already runs as expected for me with /std:c++latest and import <format>;?

0 Votes 0 ·

Your previous comments are below the answer posted by @YujianYao-MSFT

0 Votes 0 ·
YujianYao-MSFT avatar image
0 Votes"
YujianYao-MSFT answered OsmanZakir-9751 commented

Hi @OsmanZakir-9751,

I set the C++ Language Standard to /std:c++ latest, and replace import with #include, the program runs as expected, you could refer to my method to modify your code, if you still get errors, you could report the problem to Developer Community.



Best regards,


If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

set.png (27.7 KiB)
res.png (5.6 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Mine runs as expected even with the import statement if I use the /std:c++latest switch, though. I just wanted to know why it doesn't work with /std:c++20 even though support for std::format is reportedly complete in the version of the compiler I'm using.

0 Votes 0 ·

Hi @OsmanZakir-9751,

/std:c++latest and /std:c++20 are not exactly the same, std::format can only be used under /std:c++latest, please refer to this link for details.

0 Votes 0 ·

I know they're not the same. /std:c++latest is for preview. I don't think you guys understand my question. I'm trying to ask why it only works in preview mode when according to the page that talks about how much of the C++20 standard is currently supported by MSVC support for std::format is already complete under /std:c++20.

0 Votes 0 ·