boost::shared_ptr::shared_ptr(const boost::shared_

2020-02-03 06:47发布

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

标签: c++ boost c++11
1条回答
我只想做你的唯一
2楼-- · 2020-02-03 07:19

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;
查看更多
登录 后发表回答