We have a set of helper methods in base/strings/string_number_conversions.h to convert from different types of integers to string. Replacing their implementation with std::to_string didn't cause any tests to fail. Note that this only applies to integers, replacing the implementation of base::DoubleToString() does break tests.
But I found two reasons as to why we might want to disallow std::to_string():
1. Depends on std::locale for formatting. This might only apply to non-integers though.
2. It's quite slow. I did some rudimentary perf tests (
spreadsheet,
CL):
base::IntToString vs. std::to_string (100 times):
- 120.41% increase for numbers between 0 and 1,000
- 191.36% increase for numbers between -1,000 and 0
- 151.53% increase for numbers between 100,000 and 1,000,000
- 141.75% increase for numbers between -1,000,000 and -100,000
base::SizeTToString vs. std::to_string (100 times):
- 176.00% increase for numbers between 0 and 1,000
- 157.08% increase for numbers between 100,000 and 1,000,000
If we end up disallowing it, we should look into adding a presubmit check for it. IMO it is too easy to introduce uses of std::to_string as it's the first result when searching "int to string c++".
jyasskin, who also helped me think through this, mentioned that we should have a more convenient to-string function that's just no std::to_string(). Maybe we should just overload IntToString() to make it easier to use.
Gio