#include <utility>
#include <iostream>
#include <boost/optional.hpp>

template <typename T>
auto trycatch(T t) {
  return [f = std::move(t)](auto&&... args) {
    using rettype = decltype(f(std::forward<decltype(args)>(args)...));
    try {
      return boost::optional<rettype>(f(std::forward<decltype(args)>(args)...));
    }
    catch(...)
    {
      return boost::optional<rettype>{};
    }
    
  };
}

int access_resource1()
{
  throw std::runtime_error("");
}

int access_resource2()
{
  throw std::runtime_error("");
}

int calculate_smth()
{
  constexpr auto default_value = 42;
  
  auto val = trycatch(access_resource1)();
  if(!val)
  {
      val = trycatch(access_resource2)();
  }

  if(!val)
  {
      val = default_value;
  }

  return *val;
}

int main() {
  std::cout << calculate_smth() << std::endl;
}
