Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use unique_ptr for pimpl?
    primarykey
    data
    text
    <p>Here is a simplification of what I'm seeing when I try to use unique_ptr for pimpl. I chose unique_ptr because I really want the class to own the pointer - I want the lifetimes of the pimpl pointer and the class to be the same.</p> <p>Anyway, here is the header:</p> <pre><code>#ifndef HELP #define HELP 1 #include &lt;memory&gt; class Help { public: Help(int ii); ~Help() = default; private: class Impl; std::unique_ptr&lt;Impl&gt; _M_impl; }; #endif // HELP </code></pre> <p>Here is the source:</p> <pre><code>#include "Help.h" class Help::Impl { public: Impl(int ii) : _M_i{ii} { } private: int _M_i; }; Help::Help(int ii) : _M_impl{new Help::Impl{ii}} { } </code></pre> <p>I could compile these into a library just fine. But when I try to use it in a test program I get</p> <pre><code>ed@bad-horse:~/ext_distribution$ ../bin/bin/g++ -std=c++0x -o test_help test_help.cpp Help.cpp In file included from /home/ed/bin/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/memory:86:0, from Help.h:4, from test_help.cpp:3: /home/ed/bin/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/unique_ptr.h: In instantiation of 'void std::default_delete&lt;_Tp&gt;::operator()(_Tp*) const [with _Tp = Help::Impl]': /home/ed/bin/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/unique_ptr.h:245:4: required from 'void std::unique_ptr&lt;_Tp, _Dp&gt;::reset(std::unique_ptr&lt;_Tp, _Dp&gt;::pointer) [with _Tp = Help::Impl; _Dp = std::default_delete&lt;Help::Impl&gt;; std::unique_ptr&lt;_Tp, _Dp&gt;::pointer = Help::Impl*]' /home/ed/bin/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/unique_ptr.h:169:32: required from 'std::unique_ptr&lt;_Tp, _Dp&gt;::~unique_ptr() [with _Tp = Help::Impl; _Dp = std::default_delete&lt;Help::Impl&gt;]' Help.h:6:7: required from here /home/ed/bin/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/unique_ptr.h:63:14: error: invalid application of 'sizeof' to incomplete type 'Help::Impl' </code></pre> <p>This is a well known <a href="https://stackoverflow.com/questions/6012157/does-stdunique-ptrt-requires-to-know-the-full-t-definition">safety feature</a>. I've tried to follow.</p> <p>My problem is that if I put Help::Impl declaration in a header it would seem to obviate any advantage of pimpl. The class layout is visible to users. The definition is hidden but I could have done that with the Help class and private members. Also, including the declaration of Impl brings in new headers that I would have liked to keep separate.</p> <p>What am I missing? What do folks put in an Impl declaration and where? Am I doing the Help dtor wrong? Argh!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload