1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// error_code::operator bool
#include <iostream>
#include <system_error>
#include <cmath>
struct expnumber {
double value;
std::error_code error;
expnumber (double base, double exponent) {
value=pow(base,exponent);
if (errno) error.assign (errno,std::generic_category());
}
};
int main()
{
expnumber foo (3.0, 2.0), bar (3.0, 10e6);
std::cout << "foo: ";
if (!foo.error) std::cout << foo.value << std::endl;
else std::cout << "Error: " << foo.error.message() << std::endl;
std::cout << "bar: ";
if (!bar.error) std::cout << bar.value << std::endl;
else std::cout << "Error: " << bar.error.message() << std::endl;
return 0;
}
|