3 ms·
Example from the website: const std::string str { "12345678901234" }; int value = 0; std::from_chars(str.data(),str.data() + str.size(), value); On the
by vaylian 2y ago
Example from the website:
const std::string str { "12345678901234" };
int value = 0;
std::from_chars(str.data(),str.data() + str.size(), value);
On the third line: Why can I just pass in `value` like this? Shouldn't I use `&value` to pass in the output variable as a reference?
- trealira 2y ago&value would be a pointer to that integer. Instead, it's using references, which also use the & character. References use different syntax, but they're like pointers that can't be reassigned. Examples: int x = 0; int &r = x; r += 1; assert(x == 1); int y; r = y; // won't compile void inc(int &r) { r += 1; } int x = 0; inc(x); assert(x == 1); The equivalent using pointers, like in C: int x = 0; int *p = &x; *p += 1; assert(x == 1); int y; p = &y; void inc(int *p) { *p += 1; } int x = 0; inc(&x); assert(x == 1);
- trealira 2y agoMistake: r = y; // won't compile This will compile. It will be effectively the same as "x = y". The pointer equivalent is *p = y". My apologies, as it's been a while since I've used C++.
- hardlianotion 2y agoIn the function signature, value is a reference. You can think of reference as being a pointer that points to a fixed location but with value semantics. So you can dip value into the function call and the function can assign to it, as it could to data pointed to by a pointer.
- _gabe_ 2y agoI saw another commenter explain that it’s passed by reference, but I agree with you. The C++ Core Guidelines even mention that it’s better to use raw pointers (or pass by value and return a value) in cases like this to make the intent clear. https://isocpp.org/wiki/faq/references#call-by-reference https://isocpp.org/wiki/faq/references#call-by-reference
- cornstalks 2y agonit: I don't think the Core Guidelines actually suggests it's better. It's just "one style." There are pros and cons to both styles.
- nmeofthestate 2y agoA pointer parameter can be null and it doesn't make sense for this parameter to be null, so IMO a reference is the better choice here. A non-const reference is just as clear a signal that the parameter may be modified as a non-const pointer. If there's no modification const ref should be used.
- jdashg 2y agoIt's about clarity of intent at the call site. Passing by mutable ref looks like `foo`, same as passing by value, but passing mutability of a value by pointer is textually readably different: `&foo`. That's the purpose of the pass by pointer style. You could choose to textually "tag" passing by mutable ref by passing `&foo` but this can rub people the wrong way, just like chaining pointer outvars with `&out_foo`.
- jcelerier 2y agoIf you want clarity of intent define dummy in and out macros but please don't make clean reference-taking APIs a mess by turning them into pointers for no good reason
- deleted 2y ago[deleted]
- gpderetta 2y agoIn theory a from_char with an optional output parameter could be useful to validate that the next field is a number and/or discard it without needing to parse it; it might even be worth optimizing for that case.