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

Osman Zakir 121 Reputation points
2021-10-25T23:08:28.487+00:00

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.

Developer technologies | C++
{count} votes

Accepted answer
  1. RLWA32 49,636 Reputation points
    2021-10-29T09:17:37.777+00:00

    Read this document https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-160. 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.


1 additional answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2021-10-26T06:25:00.637+00:00

    Hi @Osman Zakir ,

    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.

    143680-set.png

    143682-res.png

    Best regards,

    Elya


    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.