/**************************************************************** **** **** This file belongs with the course **** Introduction to Scientific Programming in C++/Fortran2003 **** copyright 2019-2021 Victor Eijkhout eijkhout@tacc.utexas.edu **** **** optional.cxx : optional results **** ****************************************************************/ #include using std::cin; using std::cout; #include //codesnippet rootandvalid #include using std::tuple, std::pair; //codesnippet end //codesnippet rootvariantdef #include using std::variant, std::get_if; //codesnippet end #include using std::optional; //codesnippet rootorerror bool RootOrError(float &x) { if (x<0) return false; else x = std::sqrt(x); return true; }; //codesnippet end //codesnippet rootandvalid pair RootAndValid(float x) { if (x<0) return {false,x}; else return {true,std::sqrt(x)}; }; //codesnippet end //codesnippet rootvariantdef variant RootVariant(float x) { if (x<0) return false; else return std::sqrt(x); }; //codesnippet end //codesnippet mayberootptr optional MaybeRoot(float x) { if (x<0) return {}; else return std::sqrt(x); }; //codesnippet end int main() { float x; x = 2; //codesnippet rootorerror for ( auto x : {2.f,-2.f} ) if (RootOrError(x)) cout << "Root is " << x << '\n'; else cout << "could not take root of " << x << '\n'; //codesnippet end //codesnippet rootandvalid for ( auto x : {2.f,-2.f} ) if ( auto [ok,root] = RootAndValid(x) ; ok ) cout << "Root is " << root << '\n'; else cout << "could not take root of " << x << '\n'; //codesnippet end //codesnippet rootvariantuse for ( auto x : {2.f,-2.f} ) { auto okroot = RootVariant(x); auto root = get_if(&okroot); if ( root ) cout << "Root is " << *root << '\n'; auto nope = get_if(&okroot); if (nope) cout << "could not take root of " << x << '\n'; } //codesnippet end //codesnippet mayberootptr for ( auto x : {2.f,-2.f} ) if ( auto root = MaybeRoot(x) ; root.has_value() ) cout << "Root is " << root.value() << '\n'; else cout << "could not take root of " << x << '\n'; //codesnippet end return 0; }