On Saturday, 24 August 2013 at 20:11:14 UTC, H. S. Teoh wrote:
How would you use RAII to solve this problem? If I have a class:

        class C {
                Resource1 res1;
                Resource2 res2;
                Resource3 res3;
                this() {
                        ...
                }
        }

How would you write the ctor with RAII such that if it successfully inits res1 and res2, but throws before it inits res3, then only res1 and
res2 will be cleaned up?

Like someone else already proposed: Using a wrapper type W that releases the resources in it's destructor. W.init wouldn't release anything. So by definition (if I recall the rules correctly) every new instance of C would have res3 = Resource3.init prior to it's constructor called.

Now make sure that a) res3's destructor gets called (check) b) res3's destructor may be called on Resource3.init. That would be a new rule similar to 'no internal aliasing' and c) that every constructor of C either sets res3 to a destructable value or does not touch it at all ('transactional programming').







Reply via email to