top | item 41835024

(no title)

dgrunwald | 1 year ago

Caution with these functions: in most cases you need to check not only the error code, but also the `ptr` in the result. Otherwise you end up with `to_int("0x1234") == 0` instead of the expected `std::nullopt`, because these functions return success if they matched a number at the beginning of the string.

discuss

order

pilif|1 year ago

how can this be the ~5th iteration of a very wide-spread use-case and still contain a footgun?

The API looks like it's following best-practice, with a specific result type that also contains specific error information and yet, that's not enough and you still end up with edge-cases where things look like they're fine when they aren't.

amelius|1 year ago

I suppose the reason is that sometimes you want to parse more than just the number. For example, numbers separated by commas. In that case you will have to call the function repeatedly from your own parsing routine and advance the pointer.

jdbdndj|1 year ago

I don't see a foot gun, you just need to check if you have consumed the whole input. Which is the norm in nearly any streaming api

khwrht|1 year ago

That is a good question. The C++ stdlib has some truly bizarre APIs. I wonder if they should freeze std and work on std2.