Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Greetings Kind Regards I am completely befuddled re/ C++ format. The code below generates the compiler errors shown. Kindly Advise Thank You


#include <format>
#include <iostream>

void foo(std::string_view _format)
{
	std::cout << std::format(_format);
}

int main()
{
	std::string_view _format = "Hello World\n";
	std::cout << std::format(_format);
	foo(_format);
}

Build started...
1 > ------Build started : Project: ONLY_stl, Configuration : Debug x64------
1 > ONLY_stl.C++
1 > C:\Users\Bernard\source\repos\ONLY_stl\ONLY_stl.C++(6, 27) : error C7595 : 'std::_Basic_format_string<char>::_Basic_format_string' : call to immediate function is not a constant expression
1 > C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.32.31302\include\format(2959, 63) : message: failure was caused by a read of a variable outside its lifetime
1 > C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.32.31302\include\format(2959, 63) : message: see usage of '_format'
1 > C:\Users\Bernard\source\repos\ONLY_stl\ONLY_stl.C++(12, 27) : error C7595 : 'std::_Basic_format_string<char>::_Basic_format_string' : call to immediate function is not a constant expression
1 > C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.32.31302\include\format(2959, 63) : message: failure was caused by a read of a variable outside its lifetime
1 > C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.32.31302\include\format(2959, 63) : message: see usage of '_format'
1 > Done building project "ONLY_stl.vcxproj" --FAILED.
========== Build: 0 succeeded, 1 failed, 0 up - to - date, 0 skipped ==========


What I have tried:

I tried every possible type declaration for the format argument passed to foo() i.e. to wit e.g. basic_string, basic_string&, const basic_string, const basic_string&, constexpr basic_string, constexpr basic_string&, string_view, string_view&, const string_view, const string_view&, constexpr string_view, constexpr string_view&,

and of course searched internet to no avail, as surprisingly little information re/ same, in particular all demonstrations found utilize a call to format w/ an argument initialized within the call, rather than as obtained via function argument, as I prefer as shown above.
Posted
Updated 9-Jun-22 7:17am
Comments
k5054 17-Mar-22 22:23pm    
The compiler explorer (https://godbolt.org/) has no issues compiling your code. But I did need to add the /std:c++latest flag. I tested with X64 MSVC v19.29 VS 16.10 through X64 MSVC v19.latest. Maybe there's an issue with the 2022 Preview tools?
Richard MacCutchan 18-Mar-22 5:15am    
I did the same as k5054, and received the same results. I also added the following include directives:
#include <format>
#include <string_view>
Member 14138220 9-Jun-22 12:56pm    
I also got these error messages recently. I have no solution to this yet. Compiler Explorer also fails to compile this code now (with x64 MSVC v19.32, https://godbolt.org/z/o8bWjPsxP).

1 solution

The problem seems to be consteval _Basic_format_string constructor that cannot convert runtime strings like that.

std::_Basic_format_string<char> s = _format;


I went with using the underlying vformat() method that doesn't enforce this additional safety, and accepts the usual string classes just fine:

#include <iostream>
#include <format>
#include <string_view>

void foo(std::string_view _format)
{
	std::cout << std::vformat(_format, std::make_format_args());
}

int main()
{
	std::string_view _format = "Hello World\n";
	std::cout << std::vformat(_format, std::make_format_args());
	foo(_format);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900