2

Boost Intrusive ライブラリを学習しています。STL コンテナーをコピーしようとすると問題が発生します。std::vector を使用します。list_base_hookモードのクラスの要素が含まれていますauto_unlinkが、ノード ( ) に関する情報はis_linked()、コピー コンストラクターを呼び出すと失われます。

次のコードがあります。

 class helper_class
 {
     public:
         helper_class(void) { /* ... */ }
         helper_class(const helper_class& hc) { /* ... */ }
         helper_class(helper_class&& hc) { /* ... */ }
         helper_class & operator=(const helper_class& hc) { /* ... */ }
         helper_class & operator=(helper_class&& hc) { /* ... */ }
         virtual ~helper_class(void) { /* ... */ }
         // ...
 };

 typedef list_base_hook<link_mode<auto_unlink> > auto_hook;

 class my_class : public auto_hook
 {
     public:

         friend bool operator==(const my_class &a, const my_class &b)
         {
             return (a.int_ == b.int_) &&
                     (a.helper_class_ == b.helper_class_);
         }

         int int_;
         helper_class* helper_class_;

         // ...
 };

 typedef list<my_class, constant_time_size<false> >  my_class_list;

 struct new_cloner
 {
      my_class *operator()(const my_class &clone_this)
      {  return new my_class(clone_this);  }
 };

 struct delete_disposer
 {
      void operator()(my_class *delete_this)
      {  delete delete_this;  }
 };

 int main()
 {
     // ...
     helper_class the_helper_class;
     const int MaxElem = 100;
     std::vector<my_class> nodes(MaxElem);
     std::vector<my_class> copy_nodes(MaxElem);
     my_class_list list;

     for(int i = 0; i < MaxElem; ++i) {
         nodes[i].int_ = i;
         nodes[i].helper_class_ = &the_helper_class;
     }

     list.insert(list.end(), nodes.begin(), nodes.end());

     my_class_list cloned_list;
     cloned_list.clone_from(list, new_cloner(), delete_disposer());

     copy_nodes = nodes;

     std::cout  << "nodes[0].is_linked()       : "
                << ((nodes[0].is_linked()) ? "LINKED":"NO-LINKED")
                << std::endl;
     std::cout  << "copy_nodes[0].is_linked()  : "
                << ((copy_nodes[0].is_linked()) ? "LINKED":"NO-LINKED")
                << std::endl;
     std::cout  << "list[0].is_linked()        : "
                << (((*list.begin()).is_linked()) ? "LINKED":"NO-LINKED")
                << std::endl;
     std::cout  << "cloned_list[0].is_linked() : "
                << (((*cloned_list.begin()).is_linked()) ? "LINKED":"NO-LINKED")
                << std::endl;
     cloned_list.clear_and_dispose(delete_disposer());

     // ...

     return 0;
 };

標準出力:

nodes[0].is_linked()       : LINKED
copy_nodes[0].is_linked()  : NO-LINKED
list[0].is_linked()        : LINKED
cloned_list[0].is_linked() : LINKED

ベクトルcopy_nodesがリンクされていないのはなぜですか?

ありがとうございます。

4

1 に答える 1