
Ion GaztaƱaga wrote: ...
I meant that with a class that wants to contain a pure-RAII object without dynamic allocation/optional, it has to initialize the object in the member constructor list. Imagine that depending constructor arguments and the constructed other member, you want to open, open_or_create or create a RAII object. Since you have to initialize raii in the initializer list, Iyou can only use a constructor type. And for example, to open raii I need 3 arguments, and to create raii resource I need 4.
class Holder { RAII raii; public: Holder(/*some conditions*/) : raii(/*Create or open depending arguments, and other temporary results*/) {} };
If I have two phase construction (error handling omitted):
class Holder { TwoPhase twophase; public: Holder(/*some conditions*/) : raii() { /*Depending on passed conditions, open or create*/ if() twophase.open(/*3 arguments*/) else twophase.create(/*4 arguments*/) //If we throw, the twophase destructor will free resources } };
Looking at this from the RAII perspective, I'd say, hmm, what if I have a constructor that opens if the resource is already available, otherwise will ask the OS for a new one. IIRC, that's what Microsoft does for named IPC objects. This simplifies the usage of the library from the user's perpective. Jeff Flinn