1

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?

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • 1
    Possibly [relevant answer](https://stackoverflow.com/a/62596471/10871073)? – Adrian Mole Jan 15 '22 at 21:30
  • Or perhaps this is useful? [Finding the number of digits of an integer](https://stackoverflow.com/questions/6655754/finding-the-number-of-digits-of-an-integer) – BoP Jan 15 '22 at 21:38
  • @Adrian Mole Yes, might be useful. – digito_evo Jan 15 '22 at 22:27
  • `std::to_string` is specified in terms of `sprintf`. While it doesn't have to actually be implemented via `sprintf`, it more than likely shares internals with it at the very least across major implementations. Making *that* `constexpr` will be a major pain to standard library and/or compiler vendors. Don't expect it soon. – StoryTeller - Unslander Monica Jan 16 '22 at 00:31

1 Answers1

2

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?

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.

Barry
  • 286,269
  • 29
  • 621
  • 977