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.