5 ms·
> if (auto [to, ec] = std::to_chars(p, last, 42)) I'm not into plusplus, however i'm curious. How the tuple get evaluated to a condition ? is that lowered to i
by sixthDot 2y ago
> if (auto [to, ec] = std::to_chars(p, last, 42))
I'm not into plusplus, however i'm curious. How the tuple get evaluated to a condition ? is that lowered to if `to && ec` ?
- ReleaseCandidat 2y ago`ec` is an error code. What happens is a conversion to bool, see `operátor bool()` https://en.cppreference.com/w/cpp/utility/to_chars_result https://en.cppreference.com/w/cpp/utility/to_chars_result And no, don't ask mé why somebody might think that a bool is a suitable type to check for success or error.
- saagarjha 2y agoWhat is wrong with your keyboard lol
- ReleaseCandidat 2y agoAutocomplete.
- rnallandigal 2y agoThe std::to_chars function returns an object of type std::to_chars_result, which defines an operator bool() checking if ec == std::errc[0]. The if statement determines which branch to take based on the value of the condition. This value is contextually converted to a bool and evaluated[1]. [0] https://en.cppreference.com/w/cpp/utility/to_chars_result https://en.cppreference.com/w/cpp/utility/to_chars_result [1] https://en.cppreference.com/w/cpp/language/if#Condition https://en.cppreference.com/w/cpp/language/if#Condition
- quietbritishjim 2y agoBut there are two variables being defined by the destructuring. I believe OP's question was whether there's a rule for which gets chosen for the condition, rather than about contextual conversion to bool in general (which happens even when there's no initialisation in the if statement at all). Your comment seems to imply the condition is evaluated before initialising the variable(s) at all; is that what you meant? If so, this beast would work (even though it's undefined behaviour to construct a std::string from nullptr, and std::string is not convertible to bool): const char* foo() // may return nullptr if (std::string s = foo())
- wrasee 2y agoYes exactly. My hunch is to remember that in `auto [to, ec] = std::to_chars(p, last, 42)` the two names `to` and `ec` are not "real" variables/objects, but names bound to parts of the object returned to by `std::to_chars`. So fundamentally, `std::to_chars` returns a `std::to_chars_result`, that _is_ the return value and what is then contextually converted to bool for evaluation of the condition. It's then some C++17 compiler thing that separately associates the two names `to` and `ec` with the two parts of that returned tuple object. But I could be wrong, the paper for the feature is linked but I didn't read it (!).
- quietbritishjim 2y ago> Yes exactly. Yes exactly, my example would work? > My hunch is to remember that in `auto [to, ec] = std::to_chars(p, last, 42)` the two names `to` and `ec` are not "real" variables/objects, but ... Oh so my example wouldn't work after all (because std::string s is a "real" variable/object)?
- protomolecule 2y agoYour example wouldn't work, yes. In case of structured binding The decision variable of the declaration is the invented variable e introduced by the declaration. but in your case its simply: The decision variable of the declaration is the declared variable.
- alecco 2y agoWhat is assigned (std::to_chars_result) is considered by the if condition. The left hand side of the assignment is then split in two. Just like if it were if (auto res = std::to_chars(p, last, 42)). The split with the [to, ec] makes it convenient inside the if body.
- quietbritishjim 2y agoOk but you've avoided saying whether my example would work, and I don't think what you've said even hints one way or another.