4 ms·
I wonder why it is called `from_chars` and not `to_number` or similar. It's obvious what you are converting from, because you have to write down the argument `f
by captainmuon 2y ago
I wonder why it is called `from_chars` and not `to_number` or similar. It's obvious what you are converting from, because you have to write down the argument `from_chars(someString)`, but you don't see what is coming out.
- Someone 2y agoAs you indicate, you do see what you’re putting in, and that includes an argument holding a reference to the result of the conversion. What’s coming out is a std::from_chars_result: a status code plus an indicator how much of the data was consumed. What to name this depends on how you see this function. As part of a function on number types, from_chars is a good names. As part of a function on strings, to_int/to_long/etc are good names. As freestanding functions, chars_to_int (ugly, IMO), parse_int (with parse sort-of implying taking a string) are options. I can see why they went for from_chars. Implementations will be more dependent on the output type than on character pointers, it’s more likely there will be more integral types in the future than that there will be a different way to specify character sequences, and it means adding a single function name.
- pavlov 2y agoMaybe “number” is too ambiguous because they’d have to define that “in this context a number means a float or integer type only.” The C++ standard also includes support for complex numbers.
- beyondCritics 2y agoAs a matter of fact, this overloads the name and hence gives less hassle for generic coding.