I was reading an example of how to use std::from_chars
function here, when I came by the following if statment:
if(auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result); ec == std::errc()) std::cout << result << "n" "p -> "" << p << ""n";
Unfortunately I can not understand what ec == std::errc()
means and how is it the condition. Does it mean that if ec
(the possible error) is equal to default value of enum class errc
the the following line should be executed? if so isn’t that also an error?
Can someone please explain it to me?
Answer
This
if(auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result); ec == std::errc()) std::cout << result << "n" "p -> "" << p << ""n";
is similar to
auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result); if( ec == std::errc() ) std::cout << result << "n" "p -> "" << p << ""n";
Long answer .. based on manual reference the result for std::from_chars
is defined as
struct from_chars_result { const char* ptr; std::errc ec; };
and based on errc manual std::errc()
is a condition for success
OTOH the individual errors can be compared using ec.code()