Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::shared_ptr::shared_ptr(const boost::shared_ptr&)' is implicitly declared as deleted

Tags:

c++

c++11

boost

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using namespace std;

struct Node
{
    Node(int data, boost::shared_ptr<int> next = boost::make_shared<int>())
      : m_data(data), m_next(next) {}

    int m_data;
    boost::shared_ptr<int> m_next;    
};

Error: http://www.compileonline.com/compile_cpp11_online.php - Compile and Execute C++11 Online (GNU GCC version 4.7.2)

Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp: In constructor 'Node::Node(int, boost::shared_ptr)':
main.cpp:9:34: error: use of deleted function 'boost::shared_ptr::shared_ptr(const boost::shared_ptr&)'
In file included from /usr/include/boost/shared_ptr.hpp:17:0,
from main.cpp:2:
/usr/include/boost/smart_ptr/shared_ptr.hpp:168:25: note: 'boost::shared_ptr::shared_ptr(const boost::shared_ptr&)' is implicitly declared as deleted because 'boost::shared_ptr' declares a move constructor or move assignment operator

Question> I have see the post Using std::shared_ptr with clang++ and libstdc++. However, I don't know how to fix it.

The solution posted in that question is "Adding a defaulted copy constructor and copy assignment operator to shared_ptr will fix the problem."

like image 924
q0987 Avatar asked Sep 19 '13 16:09

q0987


1 Answers

This is a bug in older versions of boost::shared_ptr that makes it incompatible with C++11 compilers.

The final C++11 standard says that declaring a move constructor or move assignment operator prevents the implicit definition of a copy constructor, but older versions of boost::shared_ptr do not respect that rule and assume that a copy constructor will be implicitly defined.

You either need to upgrade to Boost version 1.48 or later, or edit the Boost headers to add this to shared_ptr:

shared_ptr(const shared_ptr&) = default;
like image 166
Jonathan Wakely Avatar answered Nov 08 '22 08:11

Jonathan Wakely