c++ - Where's the proper (resource handling) Rule of Zero? -



c++ - Where's the proper (resource handling) Rule of Zero? -

here's article talks idiom named rule of zero.

here's excerpt:

class module { public: explicit module(std::wstring const& name) : handle { ::loadlibrary(name.c_str()), &::freelibrary } {} // other module related functions go here private: using module_handle = std::unique_ptr<void, decltype(&::freelibrary)>; module_handle handle; };

it reuses unique_ptr raii features don't need care implementing daunting , verbose rule of 5 wrapper.

presented way (managing handle based resources unique_ptr, way), looks hack me, not best solution it's trying solve. many assumptions beingness implicitly assumed:

one able peer , utilize basic type #define (or typedef) handle built upon. me should hidden knowledge, , solution based exclusively on interface makes available: handle. handles, can anything, they're not required pointer types.

i'd utilize idiom, falls short in many situations stumble upon.

is handle focused raii wrapper done , usable in cool library? using such tool , i'm not aware? (i think nice have such tooling not one, many types of ownerships are)

edit 1

this not platform specific resource handles, e.g., glgenlists returns kind of handle, it's gluint, , should phone call gldeletelists on it. stated, resource handles not required pointer types, , assumption should not assumed.

edit 2

rule of zero, in former sample, employing existing tool, unique_ptr, shows nice shortcut handle management. assumptions requires makes falls short. right assumptions have handle , have resource destroying function destroys resource given handle. whether handle void *, gluint, whatever, should not matter, , worse, 1 should not required peer handle inner type. purpose of managing handles raii way, if idiom tells it's that, , can't apply in such situations, sense it's not that, @ lastly given tool.

edit 3

an illustrative situation be, let's you're in charge of using fresh 3rd party c library. contains foohandle create_foo() , void destroy_foo(foohandle). think, "let's utilize foohandle's employing rule of zero". ok, go using unique_ptr, unique_ptr inquire yourself? foohandle pointer?, utilize unique_ptr foohandle's not exposed basic type? int?, may utilize straight, or improve (re)typedef things @nicolbolas has done in answer. me, seems clear, in such trivial situation, unique_ptr showing not ideal fit managing resource handles.

disclaimer:

i've tried reformulate , improve express myself in:

is there proper 'ownership-in-a-package' 'handles' available? edit 4

i've found looking for, i've set reply in reformulated question: http://stackoverflow.com/a/14902921.

background: rule of zero

first , foremost, article much more general thought resource handle wrapper illustration mentions.

the point whenever class contains members 'non-trivial' ownership semantics, class should not responsible the mechanics of ensuring proper value semantics (copy. assign, move, destruct) of containing class. rather, each constituent should implement 'rule-of-3+' appropriate, composite class can leverage compiler-defaulted special members.

further more, new standard smart pointer types simplify task of making happen contained types. in cases members required attending pointers: std::unique_ptr, shared_ptr, weak_ptr address needs here.

for custom resource types might have write rule-of-3+ wrapper class, once. , rest of classes benefit rule-of-zero.

the bullets:

one able peer , utilize base of operations type #define (or typedef) handle built upon. me should hidden knowledge, , solution based exclusively on interface makes available, handle.

a: 90% of time can (and should) utilize std::unique_ptr<> custom deleter. knowledge , responsibility cleanup still allocator. way should.

in remaining cases, you'll have write single wrapper class specific resource isn't otherwise supported.

handles, can anything, they're not required pointer types.

they can. you'd @ e.g. boost::optional. or write wrapper. point is, want isolated resource. don't want complicate class happens own/contain such resource.

c++ c++11 raii unique-ptr

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -