News for hackers. Stuff that matters.
Re:C2x: the next real revision of the C standard by MaxBarraclough on Monday November 12, 2018 @08:58AM (#18432604)
Brief aside: Boost has a facility for doing this in C++ without declaring a new class. (It boils down to a destructor, but it's more compact.)

https://www.boost.org/doc/libs/release/libs/scope_exit/doc/h...

Re:C2x: the next real revision of the C standard by nwallin on Monday November 12, 2018 @08:17PM (#18437571)
Do they have a new version based on lambdas? The syntax of that is painful. I splatter my code with this:

  // WTFPL
  class Finally {
    std::function<void()> f;
  public:
    Finally(std::function<void()> f) : f{f} {}
    ~Finally() {f();}
    void disable() { f = [](){}; }
  };
Which looks much prettier:

  socket* s = open_socket(); // or whatever
  Finally cleanup_socket{[](){ close_socket(s);}};

  // optionally, to keep it alive:
  cleanup_socket.disable();
Am typing on mobile. No guarantees about syntax errors. I still need to look into where the right places to put r value references and/or std::move are, but for the most part, I never put anything other than a pointer in the closure so I'm not worried about copy constructor cost.
Re:C2x: the next real revision of the C standard by Matthias247 on Monday November 12, 2018 @10:05PM (#18438144)
However your implementation based on std::function might allocate, which is then non-zero cost compared to normal exit statements. Better directly store the lambda inside the class, which requires that to be generic (Finally<LambdaType>). That can be hidden by using the class via a templated function which uses types interference:

    auto cleanup_socket = make_scope_guard([&]{ close_socket(s); });
Searching for scope_guard yields lots of alternative implementations.
Re:C2x: the next real revision of the C standard by frankzinger on Tuesday November 13, 2018 @12:40AM (#18438865)
make_* functions are no longer necessary in C++17 which adds "Class template argument deduction" [1]. You can now do

  std::pair p{"aaa", 123};
and

  Finally guard{[]{cleanup();}};
without specifying template arguments.

[1] https://en.cppreference.com/w/cpp/language/class_template_ar...

Re:C2x: the next real revision of the C standard by Matthias247 on Wednesday November 14, 2018 @02:45AM (#18448254)
Thx, I wasn't aware about this feature yet!
Re:C2x: the next real revision of the C standard by aaaaaaaaaab on Monday November 12, 2018 @03:05PM (#18435237)
Boost.ScopeExit does declare a new class via macros.
Re:C2x: the next real revision of the C standard by MaxBarraclough on Monday November 12, 2018 @05:24PM (#18436524)
Yes, and I hinted at this, but it's more compact.