Search code examples
c++stdoptional

Is there another way to achieve the functionality of std::optional without another object being created?


class ScopedClass {
 public:
  ScopedClass() {
    cout << "constructor called\n";
  }

  ~ScopedClass() {
    cout << "destructor called\n";
  }

  ScopedClass(const ScopedClass &t)
  {
      cout << "copy constructor called\n";
  }

  ScopedClass(ScopedClass &&t)
  {
      cout << "move constructor called\n";
  }
}

std::optional<ScopedClass> check_and_return(bool condition) {
  if (condition == true)
    return std::make_optional(ScopedClass());
  return std::nullopt;
}

int main() {
  auto x = check_and_return(true);
}

I want a single ScopedClass object but ScopedClass() is copied/moved when I enclose it within std::optional. This means that the destructor is called when it's copied/moved and it's being called twice. Once inside check_and_return and once when main ends.

I would like the destructor to be called only when main ends.

Is there a way to achieve this? Or some alternate method not using std::optional?


Solution

  • You can use the in-place overload of std::make_optional rather than the move overload.

    std::optional<ScopedClass> check_and_return(bool condition) {
      if (condition == true)
        return std::make_optional<ScopedClass>(); // constructs ScopedClass in place with the arguments ()
      return std::nullopt;
    }