Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

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

#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."

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...