boost - Cannot iterate a flat map -
i working boost flat_map , trying iterate through it, however, cannot figure out how create iterator.
my_map = myseg.find<tlsshmmap>("temp_map").first; //fetch pointer map tlsshmemallocator alloc_inst (myseg.get_segment_manager()); (boost::container::flat_map<int, tlsstorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin(); row != my_map->end(); ++row) { //do stuff }
the 'tlsstorage' struct use store data database. boost flat map declared follows somewhere else in code:
boost::container::flat_map tls_temp_map = myseg.construct<tlsshmmap>("temp_map") (std::less<int>() ,alloc_inst); //object name
the code have above not work. here errors, ideas?
src/dbm/dbm_shm_server.cc: in member function 'int redcom::dbm::shmserver::startserver()': src/dbm/dbm_shm_server.cc:353:24: warning: unused variable 'tls_main_map' [-wunused-variable] tlsshmmap* tls_main_map; ^ src/dbm/dbm_shm_server.cc:354:24: warning: unused variable 'tls_temp_map' [-wunused-variable] tlsshmmap* tls_temp_map; ^ src/dbm/dbm_shm_server.cc: in member function 'void redcom::dbm::shmserver::fake_notify()': src/dbm/dbm_shm_server.cc:2023:84: error: value of 'alloc_inst' not usable in constant expression (boost::container::flat_map<int, tlsstorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin(); ^ src/dbm/dbm_shm_server.cc:2021:40: note: 'alloc_inst' not declared 'constexpr' const tlsshmemallocator alloc_inst (myseg.get_segment_manager()); ^ src/dbm/dbm_shm_server.cc:2023:95: error: type/value mismatch @ argument 4 in template parameter list 'template<class key, class t, class compare, class allocator> class boost::container::flat_map' (boost::container::flat_map<int, tlsstorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin(); ^ src/dbm/dbm_shm_server.cc:2023:95: error: expected type, got 'alloc_inst' src/dbm/dbm_shm_server.cc:2023:113: error: invalid type in declaration before 'row' (boost::container::flat_map<int, tlsstorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin(); ^ src/dbm/dbm_shm_server.cc:2023:113: error: expected ';' before 'row' src/dbm/dbm_shm_server.cc:2023:113: error: 'row' not declared in scope src/dbm/dbm_shm_server.cc:2024:37: error: expected ')' before ';' token row != my_map->end(); ^ src/dbm/dbm_shm_server.cc:2023:98: warning: unused variable 'const_iterator' [-wunused-variable] (boost::container::flat_map<int, tlsstorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin(); ^ src/dbm/dbm_shm_server.cc:2025:19: error: 'row' not declared in scope ++row) ^ src/dbm/dbm_shm_server.cc:2025:22: error: expected ';' before ')' token ++row) ^ distcc[31606] error: compile src/dbm/dbm_shm_server.cc on localhost failed scons: *** [debug/build/x86_64-unknown-freebsd9.2/dbm/dbm_shm_server.o] error 1 scons: building terminated because of errors.
your code appears thoroughly confused. , errors related code shown...[1]
never mind, have figured out in actual code (shmserver::fake_notify
) are declaring allocinst
, nearly showed, const
:
const tlsshmemallocator alloc_inst (myseg.get_segment_manager());
which handsomely explains why loop control variable has invalid type:
error: value of 'alloc_inst' not usable in constant expression error: expected type, got 'alloc_inst'
i mean, really, compiler couldn't more explicit it. in case wasn't clear enough added pretty ascii art:
for(flat_map<int, tlsstorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin();
so yeah... you're trying pass allocator allocator type argument? instead, use types template arguments. note use of typedefs reduce code clutter:
typedef boost::container::flat_map< int, tlsstorage, std::less<int>, tlsshmemallocator> shmtable; typedef shmtable::const_iterator shmtableit; for(shmtableit rowit=my_map->begin(); rowit!=my_map->end(); ++rowit) { shmtableit::value_type const& row = *rowit; int id = row.first; tlsstorage const& rowdata = row.second; }
now of course, c++11 write without typedefs
for(auto rowit=my_map->begin(); rowit!=my_map->end(); ++rowit) { int id = rowit->first; auto& rowdata = rowit->second; }
or more point:
for(auto const& row : *my_map) { int id = row.first; auto& rowdata = row.second; }
try reduce on cruft don't overwhelmed code :)
[1] points in case:
boost::container::flat_map template, declaration cannot possibly correct. suspect have
tlsshmmap* tls_temp_map;
why give false code? that's unrelated?
in fact,
my_map
in actual code? ortls_temp_map
? ortls_main_map
(which don't show, declared , never used...)?
Comments
Post a Comment