[bind10-dev] boost non-copyable

han benjamin ben.han.cn at gmail.com
Tue May 25 01:03:56 UTC 2010


If the code dependency is one concern, we can write our own
noncopyable base class. There is no magic in
the boost::noncopyable. It's quite easy:

class noncopyable
  {
   protected:
      noncopyable() {}
      ~noncopyable() {}
   private:  // emphasize the following members are private
      noncopyable( const noncopyable& );
      const noncopyable& operator=( const noncopyable& );
  };

There are two reasons I think we should use it.  One is to eliminate
the duplication, the other one is that
for others even they have no background knowledge about c++, he can
easily understand what your intension
when you inherit from noncopyable instead of declaring two private functions.


2010/5/25 JINMEI Tatuya / 神明達哉 <jinmei at isc.org>
>
> At Mon, 24 May 2010 19:04:20 +0800,
> han benjamin <ben.han.cn at gmail.com> wrote:
>
> > I read some code in c++ that try to make the class non-copyable.  In boost
> > there is one utility class called noncopyable, which will make the code more
> > clean and easy to read.
>
> Yeah, I know boost noncopyable.  So far, however, I've not used it
> because I wanted to minimize dependency on external libraries.  In
> particular, I'd like to minimize the use of boost specific definitions
> in public header files for wider portability.  For example, the only
> boost header file I include in DNS library header files is
> shared_ptr.hpp, while I use some other convenient things such as
> lexical_cast in .cc's.  (Ideally I'd like to eliminate the dependency
> on shared_ptr from header files, too, but that doesn't look like an
> easy change).
>
> If the introduction of the additional dependency sufficiently
> outweighs the portability concern, it may make sense.  To me shared
> pointers are crucial to make the implementation robust especially with
> exceptions (and it wouldn't make sense to develop an in-house version
> of it due to its complexity), so I've accepted its disadvantages.
>
> As for noncopyable, although it may make the code simpler, IMO the
> advantage isn't that substantial:
>
> > Class PleaseDonotCopyMe
> > {
> > Private:
> >          PleaseDonotCopyMe(const PleaseDonotCopyMe &);
> >          PleaseDonotCopyMe & operator=(const PleaseDonotCopyMe &);
> >
> > };
> >
> > Boost way:
> >
> > #include <boost/utility.hpp>
> >
> > Class PleaseDonotCopyMe : boost::noncopyable  // use private inheritance
>
> so personally I'm not convinced we should use it.
>
> Others may have different opinions, of course.  Maybe a topic for
> tomorrow's call?
>
> ---
> JINMEI, Tatuya
> Internet Systems Consortium, Inc.



More information about the bind10-dev mailing list