如何重载赋值运算符的使用自定义字符串分配 [英] How to overload the assignment operator for strings that use custom allocator

查看:203
本文介绍了如何重载赋值运算符的使用自定义字符串分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在共享内存中使用boost多指标的容器中,我使用的替换功能来更新记录,在编译的时候,编译器会抱怨运算符=暂时不present所以我重载运营商=如下面的code,但是它似乎正在抛出的编译错误是因为分配器。这是我第一次使用自定义分配共享内存。有什么特别的,我们需要做的,而超载运营商=与自定义分配器字符串?

I am trying to use boost MultiIndex container in shared memory, I am using the replace function to update the records, when trying to compile, the compiler complained about the operator = being not present so I have overloaded the operator = as shown in the code below, however it seems that the compilation errors that are being thrown are because of the allocators. This is the first time I am using custom allocator for shared memory. Is there anything special that we need to do while overloading the operator = for strings with custom allocator?

  1 #include <boost/interprocess/managed_shared_memory.hpp>
  2 #include <boost/interprocess/allocators/allocator.hpp>
  3 #include <boost/interprocess/containers/string.hpp>
  4
  5 #include <boost/multi_index_container.hpp>
  6 #include <boost/multi_index/member.hpp>
  7 #include <boost/multi_index/ordered_index.hpp>
  8 #include <iostream>
  9
 10 using namespace boost::interprocess;
 11 namespace bmi = boost::multi_index;
 12
 13 typedef managed_shared_memory::allocator<char>::type              char_allocator;
 14 typedef basic_string<char, std::char_traits<char>, char_allocator> shm_string;
 15
 16 //Data to insert in shared memory
 17 struct tickerUpdateInfo
 18 {
 19   shm_string  id;
 20   shm_string  symbol;
 21   int         last_update_time;
 22
 23   tickerUpdateInfo( const char * id_,
 24       const char *symbol_,
 25       int last_update_time_,
26       const char_allocator &a)
 27     : id(id_, a), symbol(symbol_, a), last_update_time(last_update_time_) {
 28     }
 29
 30   tickerUpdateInfo& operator=(const tickerUpdateInfo& other)
 31   {
 32     if (this != &other) {
 33       id = other.id;
 34       symbol = other.symbol;
 35       last_update_time = other.last_update_time;
 36     }
 37     return *this;
 38   }
 39 };
 40
 41 std::ostream& operator<<(std::ostream& os, const tickerUpdateInfo& obj)
 42 {
 43   // write obj to stream
 44   os << obj.id << " ";
 45   os << obj.symbol << " ";
 46   os << obj.last_update_time << " " << std::endl;
 47   return os;
 48 };
 49
 50
 51 //Tags
 52 struct id{};
 53 struct symbol{};
 54 struct last_update_time{};
 55
 56 // Define a multi_index_container of tickerUpdateInfos with following indices:
 57 //   - a unique index sorted by tickerUpdateInfo::id,
 58 //   - a unique index sorted by tickerUpdateInfo::symbol,
 59 //   - a non-unique index sorted by tickerUpdateInfo::last_update_time.
 60 typedef bmi::multi_index_container<
 61 tickerUpdateInfo,
 62   bmi::indexed_by<
 63   bmi::ordered_unique
 64   <bmi::tag<id>,  BOOST_MULTI_INDEX_MEMBER( tickerUpdateInfo, shm_string, id)>,
 65   bmi::ordered_unique<
 66   bmi::tag<symbol>,BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, shm_string, symbol)>,
 67   bmi::ordered_non_unique
 68   <bmi::tag<last_update_time>, BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, int, last_update_time)> >,
 69   managed_shared_memory::allocator<tickerUpdateInfo>::type
 70   > tickerUpdateInfo_set;
 71
 72 int main ()
 73 {
 74   //Remove shared memory on construction and destruction
 75   struct shm_remove
 76   {
 77     shm_remove() { shared_memory_object::remove("MySharedMemory"); }
 78     ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
 79   } remover;
 80
 81   //Create shared memory
 82   managed_shared_memory segment(create_only,"MySharedMemory", 65536);
 83
 84   //Construct the multi_index in shared memory
 85   tickerUpdateInfo_set *es = segment.construct<tickerUpdateInfo_set>
 86     ("TickerUpdateContainer")            //Container's name in shared memory
 87     ( tickerUpdateInfo_set::ctor_args_list()
 88       , segment.get_allocator<tickerUpdateInfo>());  //Ctor parameters
 89
 90   //Now insert elements
 91   char_allocator ca(segment.get_allocator<char>());
 92   es->insert(tickerUpdateInfo("0","Joe", 31,ca));
 93   es->insert(tickerUpdateInfo("1", "Robert",27, ca));
 94   es->insert(tickerUpdateInfo( "2","John", 36,ca));
 95   const tickerUpdateInfo_set::nth_index<1>::type& name_index = (*es).get<1>();
 96   std::cout << "Before update " << std::endl;
 97   std::copy(
 98       name_index.begin(),name_index.end(),
 99       std::ostream_iterator<tickerUpdateInfo>(std::cout));
100
101
102   typedef tickerUpdateInfo_set::index<symbol>::type ticker_update_info_set_by_symbol;
103   ticker_update_info_set_by_symbol & nm_index = (*es).get<symbol>();
104   ticker_update_info_set_by_symbol::iterator it=nm_index.find("Joe");
105   tickerUpdateInfo ticker_info = *it;
106   ticker_info.symbol = "Deb";      // update key
107   nm_index.replace(it, ticker_info ); // update her record
108   std::cout << "After update " << std::endl;
109   std::copy(
110       nm_index.begin(),nm_index.end(),
111       std::ostream_iterator<tickerUpdateInfo>(std::cout));
112   return 0;
113 }

编译错误:

   -- Compiling src/writer.cxx
In file included from include/boost/multi_index/ordered_index.hpp:56,
                 from src/writer.cxx:7:
include/boost/multi_index/detail/ord_index_ops.hpp: In constructor 'boost::container::basic_string<CharT, Traits, Alloc>::basic_string(const CharT*, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]':
include/boost/multi_index/detail/ord_index_ops.hpp:67:   instantiated from 'Node* boost::multi_index::detail::ordered_index_find(Node*, Node*, const KeyFromValue&, const CompatibleKey&, const CompatibleCompare&) [with Node = boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<tickerUpdateInfo, boost::interprocess::allocator<tickerUpdateInfo, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > > >, KeyFromValue = boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, CompatibleKey = char [4], CompatibleCompare = std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >]'
include/boost/multi_index/ordered_index.hpp:434:   instantiated from 'boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<typename SuperMeta::type::node_type> > boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::find(const CompatibleKey&) const [with CompatibleKey = char [4], KeyFromValue = boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, Compare = std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >, SuperMeta = boost::multi_index::detail::nth_layer<2, tickerUpdateInfo, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<id, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::id>, mpl_::na>, boost::multi_index::ordered_unique<boost::multi_index::tag<symbol, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, mpl_::na>, boost::multi_index::ordered_non_unique<boost::multi_index::tag<last_update_time, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, int, &tickerUpdateInfo::last_update_time>, mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::interprocess::allocator<tickerUpdateInfo, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, TagList = boost::mpl::v_item<symbol, boost::mpl::vector0<mpl_::na>, 0>, Category = boost::multi_index::detail::ordered_unique_tag]'
src/writer.cxx:107:   instantiated from here
include/boost/multi_index/detail/ord_index_ops.hpp:67: error:  no matching function for call to 'boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >::allocator()'
include/boost/interprocess/allocators/allocator.hpp:130: note: candidates are: boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T, SegmentManager>&) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>]
include/boost/interprocess/allocators/allocator.hpp:125: note: boost::interprocess::allocator<T, SegmentManager>::allocator(SegmentManager*) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>]
include/boost/multi_index/detail/ord_index_ops.hpp:74:   instantiated from 'Node* boost::multi_index::detail::ordered_index_find(Node*, Node*, const KeyFromValue&, const CompatibleKey&, const CompatibleCompare&) [with Node = boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<tickerUpdateInfo, boost::interprocess::allocator<tickerUpdateInfo, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > > >, KeyFromValue = boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, CompatibleKey = char [4], CompatibleCompare = std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >]'
include/boost/multi_index/ordered_index.hpp:434:   instantiated from 'boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<typename SuperMeta::type::node_type> > boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::find(const CompatibleKey&) const [with CompatibleKey = char [4], KeyFromValue = boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, Compare = std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >, SuperMeta = boost::multi_index::detail::nth_layer<2, tickerUpdateInfo, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<id, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::id>, mpl_::na>, boost::multi_index::ordered_unique<boost::multi_index::tag<symbol, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, mpl_::na>, boost::multi_index::ordered_non_unique<boost::multi_index::tag<last_update_time, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, int, &tickerUpdateInfo::last_update_time>, mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::interprocess::allocator<tickerUpdateInfo, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, TagList = boost::mpl::v_item<symbol, boost::mpl::vector0<mpl_::na>, 0>, Category = boost::multi_index::detail::ordered_unique_tag]'
src/writer.cxx:107:   instantiated from here
include/boost/multi_index/detail/ord_index_ops.hpp:74: error:  no matching function for call to 'boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >::allocator()'
include/boost/interprocess/allocators/allocator.hpp:130: note: candidates are: boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T, SegmentManager>&) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>]
include/boost/interprocess/allocators/allocator.hpp:125: note:                 boost::interprocess::allocator<T, SegmentManager>::allocator(SegmentManager*) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>]

更新

Update

我已经改变了code位的结构和使用的仿函数作为被你的建议,我还是看到了问题,我在下面粘贴的code片段:
//TickerInfoMangerImplementation.cxx

I have changed the structure of the code a bit and used the functor as was suggested by you, I still see the problem, I am pasting the code snippets below: //TickerInfoMangerImplementation.cxx

      1 #include <TickerInfoMangerImplementation.h>
      2 #include <boost/interprocess/managed_shared_memory.hpp>
      3 #include <iostream>
      4
      5 using namespace boost::interprocess;
      6
      7 tickerInfoMangerImplementation::tickerInfoMangerImplementation( const sharedMemoryNameT & name ): m_name(name),
      8   m_managed_memory_segment( create_only, "test", 65536 )
      9 {
     10
     11   p_ticker_info_set = m_managed_memory_segment.construct<ticker_update_info_set>
     12     ("SetOfTickerUpdateInformation")            //Container's name in shared memory
     13     ( ticker_update_info_set::ctor_args_list()
     14       , m_managed_memory_segment.get_allocator<tickerUpdateInfoT>());  //Ctor parameters
     15 }
     16
     17 bool tickerInfoMangerImplementation::put_records( const tickerUpdateInfoT & record ) {
     18
     19   std::pair<ticker_update_info_set::iterator, bool> result_pair = p_ticker_info_set->insert( record );
     20   if( result_pair.second ) {
     21     return result_pair.second;
     22   }
     23
     24   typedef ticker_update_info_set::index<symbol_index>::type ticker_update_info_set_by_symbol;
     25   ticker_update_info_set_by_symbol & sym_index = (*p_ticker_info_set).get<symbol_index>();
     26   ticker_update_info_set_by_symbol::iterator it = sym_index.find( record.m_symbol );
     27   tickerUpdateInfoT ticker_info = *it;
     28   ticker_info.m_last_update_time = record.m_last_update_time;
     29   return sym_index.replace( it, ticker_info );
     30 }
     31
     32 int tickerInfoMangerImplementation::get_active_ticker_count( const thresholdT seconds ) {
     33 }
     34
     35 void tickerInfoMangerImplementation::print_contents() {
     36   const ticker_update_info_set::nth_index<1>::type& name_index = (*p_ticker_info_set).get<1>();
     37   std::copy( name_index.begin(), name_index.end(), std::ostream_iterator<tickerUpdateInfoT>(std::cout) );
     38 }
     39
     40 std::ostream& operator<<(std::ostream& os, const tickerUpdateInfoT & obj) {
     41   os << obj.m_id << " ";
     42   os << obj.m_symbol << " ";
     43   os << obj.m_last_update_time << " " << "\n";
     44   return os;
     45 };

//TickerInfoMangerImplementation.h

      1 #ifndef __TICKER_INFO_MANAGER_IMPL__
  2 #define __TICKER_INFO_MANAGER_IMPL__
  3
  4 #include <boost/interprocess/containers/string.hpp>
  5 #include <boost/interprocess/shared_memory_object.hpp>
  6 #include <boost/multi_index_container.hpp>
  7 #include <boost/multi_index/member.hpp>
  8 #include <boost/multi_index/ordered_index.hpp>
  9 #include <TickerInfoManagerConstants.h>
 10 #include <TickerInfo.h>
 11
 12 namespace bmi = boost::multi_index;
 13 namespace bip = boost::interprocess;
 14
 15 struct id_index{};
 16 struct symbol_index{};
 17 struct last_update_time_index{};
 18
 19 struct Less {
 20   template<class T, class U>
 21     bool operator()(T const& t, U const& u) const {
 22       return t < u;
 23     }
 24 };
 25
 25
 26
 27 typedef bmi::multi_index_container<
 28 tickerUpdateInfoT,
 29   bmi::indexed_by<
 30   bmi::ordered_unique
 31   <bmi::tag<id_index>,  BOOST_MULTI_INDEX_MEMBER( tickerUpdateInfo, shm_string, m_id), Less>,
 32   bmi::ordered_unique<
 33   bmi::tag<symbol_index>,BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, shm_string, m_symbol), Less>,
 34   bmi::ordered_non_unique
 35   <bmi::tag<last_update_time_index>, BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, int, m_last_update_time), Less> >,
 36   bip::managed_shared_memory::allocator<tickerUpdateInfo>::type
 37   > ticker_update_info_set;
 38
 39   class tickerInfoMangerImplementation {
 40
 41     public:
 42       tickerInfoMangerImplementation( const sharedMemoryNameT & name );
 43
 44       bool put_records( const tickerUpdateInfoT & record );
 45
 46       int get_active_ticker_count( const thresholdT seconds );
 47
 48       void print_contents();
 49
 50       bip::managed_shared_memory& get_managed_memory_segment() {
 51         return m_managed_memory_segment;
 52       }
 53
 54     private:
 55       const sharedMemoryNameT    m_name;
 56       bip::managed_shared_memory m_managed_memory_segment;
 57       ticker_update_info_set     *p_ticker_info_set;
 58   };
 59 #endif

// TickerInfo.h

//TickerInfo.h

  1 #ifndef __TICKER_INFO__
  2 #define __TICKER_INFO__
  3
  4 #include <boost/interprocess/managed_shared_memory.hpp>
  5 #include <boost/interprocess/allocators/allocator.hpp>
  6 #include <boost/interprocess/containers/string.hpp>
  7
  8 typedef boost::interprocess::managed_shared_memory::allocator<char>::type               char_allocator;
  9 typedef boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator> shm_string;
 10
 11 //Data to insert in shared memory
 12 typedef struct tickerUpdateInfo{
 13
 14   shm_string  m_id;
 15   shm_string  m_symbol;
 16   int         m_last_update_time;
 17
 18   tickerUpdateInfo( const char * id,
 19       const char *symbol,
 20       int last_update_time,
 21       const char_allocator &a)
 22     : m_id( id, a), m_symbol( symbol, a), m_last_update_time( last_update_time) {
 23     }
 24 } tickerUpdateInfoT;
 25

错误日志:

tor0,0> =类的boost :: multi_index ::详细:: ordered_unique_tag]'
/home/user/droy/src/quotes/debshmutils/shmdb/src/TickerInfoMangerImplementation.cxx:29:从这里实例化
/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/multi_index/detail/index_base.hpp:114:错误:不对应的操作符='在'X->的boost :: multi_index ::详细:: index_node_base ::与值= tickerUpdateInfo,分配器=的boost ::进程间::分配器,价值0ul>,提高::进程间: :iset_index>> = v'
包括/ TickerInfo.h:12:注意:考生:tickerUpdateInfo&放大器; tickerUpdateInfo ::运算符=(tickerUpdateInfo&安培;)

tor0, 0>, Category = boost::multi_index::detail::ordered_unique_tag]' /home/user/droy/src/quotes/debshmutils/shmdb/src/TickerInfoMangerImplementation.cxx:29: instantiated from here /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/multi_index/detail/index_base.hpp:114: error: no match for 'operator=' in 'x->boost::multi_index::detail::index_node_base::value with Value = tickerUpdateInfo, Allocator = boost::interprocess::allocator, 0ul>, boost::interprocess::iset_index> > = v' include/TickerInfo.h:12: note: candidates are: tickerUpdateInfo& tickerUpdateInfo::operator=(tickerUpdateInfo&)

请容忍我,因为我以什么怎么回事并不清楚。谢谢!

Please bear with me as I am not clear as to whats going on here. Thank You!

推荐答案

的问题是, ordered_unique / ordered_non_unique 索引使用的std ::以下&LT; K&GT; 默认情况下。当您通过一个兼容键 nm_index.find(乔)字符常量[] 乔的需求搜索在 STD转换为 shm_string ::以下&LT; K&GT; ::运算符()(shm_string常量和放大器;,shm_string常量和放大器;),但 shm_string 构造也需要一个分配器参数。

The problem is that ordered_unique/ordered_non_unique indexes use std::less<K> by default. When you search by a compatible key nm_index.find("Joe"), char const[] "Joe" needs to be converted to shm_string in std::less<K>::operator()(shm_string const&, shm_string const&), but shm_string constructor also requires an allocator argument.

解决方案是提供自己的比较类不转换参数:

The solution is to provide your own compare class that does not convert arguments:

struct Less
{
    template<class T, class U>
    bool operator()(T const& t, U const& u) const {
        return t < u;
    }
};

// Define a multi_index_container of tickerUpdateInfos with following indices:
//   - a unique index sorted by tickerUpdateInfo::id,
//   - a unique index sorted by tickerUpdateInfo::symbol,
//   - a non-unique index sorted by tickerUpdateInfo::last_update_time.
typedef bmi::multi_index_container<
    tickerUpdateInfo,
    bmi::indexed_by<
          bmi::ordered_unique<bmi::tag<id>, BOOST_MULTI_INDEX_MEMBER( tickerUpdateInfo, shm_string, id), Less>,
          bmi::ordered_unique<bmi::tag<symbol>,BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, shm_string, symbol), Less>,
          bmi::ordered_non_unique<bmi::tag<last_update_time>, BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, int, last_update_time), Less>
          >,
    managed_shared_memory::allocator<tickerUpdateInfo>::type
> tickerUpdateInfo_set;


tickerUpdateInfo&安培;运算符=(常量tickerUpdateInfo&安培;其他)。不是必需的,编译器生成的人做同样的事情。


tickerUpdateInfo& operator=(const tickerUpdateInfo& other) is not required, the compiler generated one does the same thing.

这篇关于如何重载赋值运算符的使用自定义字符串分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆