#include <iostream>
#include <memory>

template<typename F>
[[nodiscard]] auto im_finally(F f, bool active = true) noexcept(noexcept(F(std::move(f)))) {
  auto x = [f = std::move(f)](void*){ f(); };
  return std::unique_ptr<void, decltype(x)>((void*)(active), std::move(x));
}

inline [[nodiscard]] auto im_test_true() {
  return im_finally([]{ std::cout << "true ~dtor" << std::endl; }, true);
}

inline [[nodiscard]] auto im_test_false() {
  return im_finally([]{ std::cout << "false ~dtor" << std::endl; }, false);
}

int main() {
	if (auto false_guard = im_test_false())
		std::cout << "false operator bool()" << std::endl;
		
	if (auto true_guard = im_test_true())
	std::cout << "true operator bool()" << std::endl;
	
	return 0;
}