C++ 如何在两个boost::intrusive::slist对象之间传输节点

C++ 如何在两个boost::intrusive::slist对象之间传输节点,c++,boost,linked-list,c++17,boost-intrusive,C++,Boost,Linked List,C++17,Boost Intrusive,在两个boost::intrusive::slist对象之间传输节点有效吗?类似于下面的内容 auto one = boost::intrusive::slist<Node, boost::intrusive::cache_last<true>>{}; auto two = boost::intrusive::slist<Node, boost::intrusive::cache_last<true>>{}; auto node = std::m

在两个
boost::intrusive::slist
对象之间传输节点有效吗?类似于下面的内容

auto one = boost::intrusive::slist<Node, boost::intrusive::cache_last<true>>{};
auto two = boost::intrusive::slist<Node, boost::intrusive::cache_last<true>>{};

auto node = std::make_unique<Node>();
one.push_back(*node);

auto& front = one.front();
one.pop_front();
two.push_back(front);
autoone=boost::intrusive::slist{};
auto-two=boost::intrusive::slist{};
auto node=std::make_unique();
一、推回(*节点);
auto&front=1.front();
一、波普(前);
二、推回(前);
我在boost版本1.70.0中遇到了一个分段错误和一个断言错误。我怎样才能解决这个问题



注意:我无法分配新节点并复制旧节点,因为我正在使用侵入式列表来控制分配发生的时间和地点

这似乎是该方法的目的:

效果:将列表x中的所有元素转移到此列表中,在其指向的元素之前。不调用析构函数或复制构造函数

完整工作演示:

#include <iostream>
#include <string>
#include <boost/intrusive/slist.hpp>

struct Node : public boost::intrusive::slist_base_hook<>
{
    int         i;
    std::string s;

    Node(int i, std::string s) : i(i), s(std::move(s)) {}
};

using NodeList = boost::intrusive::slist<Node, boost::intrusive::cache_last<true>>;

int main()
{
    NodeList one;
    NodeList two;

    auto show = [&](auto text){
        std::cout <<text <<"\n  one\n";
        for (auto & item : one) { std::cout <<"    " <<item.i <<' ' <<item.s <<'\n'; }
        std::cout <<"  two\n";
        for (auto & item : two) { std::cout <<"    " <<item.i <<' ' <<item.s <<'\n'; }
    };


    one.push_back(*new Node(42, "hello"));
    show("before splice");

    two.splice(two.end(), one, one.begin());
    show("after splice");

    one.clear_and_dispose([](Node * ptr){ delete ptr; });
    two.clear_and_dispose([](Node * ptr){ delete ptr; });
    return 0;
}

您正在删除仍在容器中的节点——安全链接模式将对此进行检查并断言

如果您先删除容器(对对象构造重新排序),或者在清理之前从容器中删除节点,则不会出现断言

e、 g

auto node=std::make_unique();
autoone=boost::intrusive::slist{};
auto-two=boost::intrusive::slist{};
一、推回(*节点);
auto&front=1.front();
一、波普(前);
二、推回(前);

对不起,我删除了我的答案。当你推回时,你正在解除引用。我认为这一天发生了什么事。我让它工作,但不能删除唯一的ptr有相同的问题:@oblivion抱歉,我忘了提到,我需要转移节点。我特别希望避免为了将节点从一个节点转移到另一个节点而分配新节点。您可以使用ref或shared_ptr。
#include <iostream>
#include <string>
#include <boost/intrusive/slist.hpp>

struct Node : public boost::intrusive::slist_base_hook<>
{
    int         i;
    std::string s;

    Node(int i, std::string s) : i(i), s(std::move(s)) {}
};

using NodeList = boost::intrusive::slist<Node, boost::intrusive::cache_last<true>>;

int main()
{
    NodeList one;
    NodeList two;

    auto show = [&](auto text){
        std::cout <<text <<"\n  one\n";
        for (auto & item : one) { std::cout <<"    " <<item.i <<' ' <<item.s <<'\n'; }
        std::cout <<"  two\n";
        for (auto & item : two) { std::cout <<"    " <<item.i <<' ' <<item.s <<'\n'; }
    };


    one.push_back(*new Node(42, "hello"));
    show("before splice");

    two.splice(two.end(), one, one.begin());
    show("after splice");

    one.clear_and_dispose([](Node * ptr){ delete ptr; });
    two.clear_and_dispose([](Node * ptr){ delete ptr; });
    return 0;
}
before splice
  one
    42 hello
  two
after splice
  one
  two
    42 hello
auto node = std::make_unique<Node>();
auto one = boost::intrusive::slist<Node, boost::intrusive::cache_last<true>>{};
auto two = boost::intrusive::slist<Node, boost::intrusive::cache_last<true>>{};

one.push_back(*node);

auto& front = one.front();
one.pop_front();
two.push_back(front);