Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) boost::noncopyable

 

boost::noncopyable is a Boost base class. If any class derives from boost::noncopyable, this class cannot be copied anymore.

 

#include <boost/noncopyable.hpp>

struct Test : public boost::noncopyable
{
  int x;
};

int main()
{
  Test t1;
  Test t2;
  t2 = t1; //Not allowed by boost::noncopyable
}

 

 

 

 

 

Testing boost::noncopyable

 

The code below shows a C++98 and C++11 definition of boost::noncopyable.

 

 

struct NoncopyableCpp98
{
  protected:
    NoncopyableCpp98()  {}
    ~NoncopyableCpp98() {}
  private:
    NoncopyableCpp98(const NoncopyableCpp98&);
    const NoncopyableCpp98& operator=(const NoncopyableCpp98&);
};

struct NoncopyableCpp0x
{
  protected:
    NoncopyableCpp0x()  {}
    ~NoncopyableCpp0x() {}
  private:
    NoncopyableCpp0x(const NoncopyableCpp0x&) = delete;
    const NoncopyableCpp0x& operator=(const NoncopyableCpp0x&) = delete;
};

struct TestCpp98 : public NoncopyableCpp98
{
  int x;
};

struct TestCpp0x : public NoncopyableCpp0x
{
  int x;
};

#include <boost/noncopyable.hpp>

struct TestBoost : public boost::noncopyable
{
  int x;
};

int main()
{
  //TestCpp98 t1; TestCpp98 t2; t2 = t1; //Not allowed by NoncopyableCpp98
  //TestCpp0x t1; TestCpp0x t2; t2 = t1; //Not allowed by NoncopyableCpp0x
  //TestBoost t1; TestBoost t2; t2 = t1; //Not allowed by boost::noncopyable
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict