c++boostnoncopyable

why does boost::noncopyable require inheritance


Adding any noncopyable member to a class would prevent the automatic generation of copy construction and assignment operator. Why does boost require inheritance to use noncopyable?

I think I am not alone in my stylistic preference for

class MyUtility : public MyBase
{
   noncopyable guard;
   ...
};

as opposed to

class MyUtility : public MyBase , private noncopyable
{
   ...
};

Dave Abrahams is a smart guy, so he probably considered this possibility. What am I missing? What does inheritence accomplish?


Solution

  • Because sizeof(boost::noncopyable)!=0. So in this case your class size will be bigger.

    Here you can read about the empty base optimization. (look at section "4.7: The Empty Member Optimization").

    Edit: The fact, that noncopyable doesn't have public constructors makes it useless for any other use, while classes with public constructor could also be used for other wrong purposes. This is a another reason, why boost chose this approach.