Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-53021

Directly define ScopeGuards: sunset makeGuard and ON_BLOCK_EXIT

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Service Arch

      KISS principle. There are 2 obsolete helpers used to define ScopeGuard variables. To define a ScopeGuard variable, you should write a ScopeGuard variable defintion. That's it. It's not special or magical, but these helpers give the illusion that it could be, and this is a distraction.

      makeGuard 203 occurrences.
      ON_BLOCK_EXIT 243 occurrences.

      We have CTAD now and can replace both kinds of wrappers with a more conventional variable definition.

      ScopeGuard sg([&] {...});
      // or
      auto sg = ScopeGuard([&] {...});
      

      makeGuard is just a function to deduce the F for a ScopeGuard<F> definition. CTAD in C++17 makes most such "maker" functions obsolete.

      The only benefit of ON_BLOCK_EXIT at this point is to construct a dummy variable name to hold the makeGuard result. The other benefit it once had was to enforce that the object is bound to a variable, but [[nodiscard]] now does this for us.

      Engineers have to make up dummy variable names for other RAII objects all the time, and they manage to do it without using different macros for each RAII type. ScopeGuard definitions are not different from those other types. Yet the macro-use convention hints that something more is happening behind the scenes. It is not, so the macro is a redundant "moving part" to be eliminated.

      PS: A change was introduced to ScopeGuard that breaks CTAD but it could be brought back by reverting it or by adding this explicit deduction guide.

      template <typename F>
      ScopeGuard(F&&) -> ScopeGuard<std::decay_t<F>>;
      

            Assignee:
            backlog-server-servicearch [DO NOT USE] Backlog - Service Architecture
            Reporter:
            billy.donahue@mongodb.com Billy Donahue
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: