August 2017
Intermediate to advanced
438 pages
14h 19m
English
Ladies and gentlemen, let me introduce you to the Boost.Optional library using the following example:
The try_lock_device() function tries to acquire a lock for a device and may succeed or not, depending on different conditions (in our example it depends on some try_lock_device_impl() function call):
#include <boost/optional.hpp>#include <iostream>class locked_device { explicit locked_device(const char* /*param*/) { // We have unique access to device. std::cout << "Device is locked\n"; } static bool try_lock_device_impl();public: void use() { std::cout << "Success!\n"; } static boost::optional<locked_device> try_lock_device() { if (!try_lock_device_impl()) { // Failed to lock device. return boost::none; } // Success! return ...Read now
Unlock full access