ddrcoder•

The with statement provided a way to explicitly manage the lifetime of an object in Python. This is the default behavior for C++, all the time.

I'm not sure how much you gain over

   {
inc_dec x;
cout << "hello" << i << "\n";
   }
12
notlostyetOP•

It's not intended to be a huge win. I wrote it as an exercise in response to a comment on HN.

The primary motivation, much like SCOPE_EXIT, is in eliminating the need declare a guard object explicitly. Even in C++11, the closest scope exit implementation equivalent is something like

{
     ++i;
     ON_SCOPE_EXIT (--i);
     std::cout << i << std::endl;
}

which, while flexible, looks unnatural and doesn't make transactional intent as clear with respect to the scoping. It also has the side benefit of forcing guard objects to be templates if you want to use it.

3
LeszekSwirski•

But, given that with your method you still have to define int_dec, what's the advantage over the following?

int main() {
    int i = 0;
    std::cout << i << std::endl;
    {
        int_dec i_dec(i);
        std::cout << i << std::endl;
    }
    std::cout << i << std::endl;
}

I guess there's the variable name and the extra line of code, but on the other hand there's no macro or weird if-based scoping tricks.

2
Bolitho•

The with statement provided a way to explicitly manage the lifetime of an object in Python.

This is not true! The goal has been to introduce a context for execution blocks. In fact you can use this to ensure the reliable execution of something before an object will be destroyed, which is of course similar to a dtor in C++. But it has nothing to do with lifetime in general (which you cannot control in Python at all!)

This is the default behavior for C++, all the time.

This is not true either, as RAII holds not when dealing with new!

2
ddrcoder•

Right, by default I mean simple variable definitions on the stack. RAII is something you have to take an explicit action to avoid (namely, heap allocation).

Reliable destruction and explicitly scoped exception handling is very much about lifetimes.

3
Bolitho•

Right, by default I mean simple variable definitions on the stack. RAII is something you have to take an explicit action to avoid (namely, heap allocation).

Ok. That sounds plausible :-)

Reliable destruction and explicitly scoped exception handling is very much about lifetimes.

Yes. But that is neither the primary intention nor possible in Python. You cannot control object destruction at all - there is even no guarantee, that an object gets destructed at all during runtime of the program.

So the statements are right, but the conclusion that with in Python deals about this is wrong ;-)

1
xon_xoff•

As much of a hack as this is, I've used a macro like this to implement Java-like synchronized() syntax for mutex locks in heavily multithreaded code. It gives slightly more natural syntax than plain RAII.

Using a for() has the downside that 'break' and 'continue' don't work as expected within the scope. It is possible to work around this by creatively abusing an if() statement, however:

if (const auto& scope_wrapper_ = wrapper<scope_type>())
    (void)scope_wrapper_;
else
    <controlled block here>

Strangely, VS2015 seems to require a copy constructor here while GCC and Clang don't.

1
notlostyetOP•

Good catch wrt the break and continue statements... that's pretty embarassing. I've updated it in line with your suggestion. My only concern is whether or not the compiler, seeing the boolean conversion operator, is allowed to remove the construction and destruction of the guard object entirely...

1
xon_xoff•

I don't think so, since creation of the temporary object has side effects and the lifetime of the temporary object is extended by the named reference. Would love to hear from an expert if that is wrong.

1
devel_watcher•

RAII is unnatural for Pyton, so it has a word "with" for it. Nothing to see here.

-1