non-'constexpr' function std::to_string(int) in a consteval lambda
I'm having trouble with std::to_string()
in the following lambda function:
#include <iostream>
#include <string>
inline constexpr int a_global_constant { 12345 };
int main( )
{
auto calculateMsgLength = [ ] ( ) consteval -> std::size_t
{
std::string str1 { "Part 1 " };
std::string str2 { "Part 2 " };
std::string msg { str1 + str2 /*+ std::to_string( a_global_constant )*/ };
// The complete message should look like this: Part 1 Part 2 12345
return msg.length( );
};
constexpr std::size_t msgLength { calculateMsgLength( ) };
std::cout << "Message length == " << msgLength << '\n';
}
The above code doesn't compile on my GCC v11.2 therefore I had to use GCC (trunk) at the Compiler Explorer to compile it.
However, just by uncommenting the call to std::to_string()
it doesn't compile:
<source>: In function 'int main()':
<source>:19:61: error: 'main()::<lambda()>' called in a constant expression
19 | constexpr std::size_t msgLength { calculateMsgLength( ) };
| ~~~~~~~~~~~~~~~~~~^~~
<source>:10:35: note: 'main()::<lambda()>' is not usable as a 'constexpr' function because:
10 | auto calculateMsgLength = [ ] ( ) consteval -> std::size_t
| ^
<source>:10:35: error: call to non-'constexpr' function 'std::string std::__cxx11::to_string(int)'
.
.
.
Is std::to_string
going to be made constexpr
in STL sometime in the near future? Or should I look for an alternative way for doing this? What is an alternative to std::to_string
?
Solution 1:
Is
std::to_string
going to be made constexpr in STL sometime in the near future? Or should I look for an alternative way for doing this? What is an alternative tostd::to_string
?
I'm not aware of plans for std::to_string
, but std::to_chars
(and std::from_chars
) for integral types will likely be constexpr
in C++23 by way of P2291. Not sure why not std::to_string
, at least for the integral types.
That said, if all you want is to convert a base-10 integer to a std::string
at compile time, that's a fairly easy algorithm to write. The only tricky part is handling INT_MIN
(since you can't just negate the argument if it's negative, that would overflow). But since this is a constexpr
function, if you get it wrong, it's a compile error, which makes it a lot easier to get it right.