0

I am working with a boost flat_map and trying to iterate through it, however, I cannot figure out how to create the iterator.

 my_map = mySeg.find<tlsSHMMap>("temp_map").first;   //fetch a pointer to the map
 tlsShmemAllocator alloc_inst (mySeg.get_segment_manager());


 for (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' is a struct that I use to store data from a database. The boost flat map is declared as follows somewhere else in the code:

boost::container::flat_map tls_temp_map = mySeg.construct<tlsSHMMap>("temp_map")    (std::less<int>() ,alloc_inst);     //object name

The code that I have above does not work. Here are the errors, any 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: the value of 'alloc_inst' is not usable in a constant expression
                 for (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' was not declared 'constexpr'
                const tlsShmemAllocator alloc_inst (mySeg.get_segment_manager());
                                        ^
src/dbm/dbm_shm_server.cc:2023:95: error: type/value mismatch at argument 4 in template parameter list for 'template<class Key, class T, class Compare, class Allocator> class boost::container::flat_map'
                 for (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 a type, got 'alloc_inst'
src/dbm/dbm_shm_server.cc:2023:113: error: invalid type in declaration before 'row'
                 for (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' was not declared in this 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]
                 for (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' was not declared in this 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.
user3208010
  • 45
  • 1
  • 8
  • Please use self-contained example code: http://sscce.org/ So questions should not be code puzzles, if you expect them to be answered – sehe Jan 31 '14 at 23:15

1 Answers1

2

Your code appears to be thoroughly confused. And the errors only slightly related to the code shown...[1]

Never mind, I have figured out that in your actual code (ShmServer::fake_notify) you are declaring allocInst, nearly like you showed, but const:

const tlsShmemAllocator alloc_inst (mySeg.get_segment_manager());

Which also handsomely explains why your loop control variable has an invalid type:

error: the value of 'alloc_inst' is not usable in a constant expression
error:   expected a type, got 'alloc_inst'

I mean, really, the compiler couldn't be much more explicit about it. In case this wasn't clear enough it 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 to pass an allocator as an allocator type argument? Instead, use types as template arguments. Note the use of typedefs to 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, with C++11 you could write that without all those typedefs

for(auto rowit=my_map->begin(); rowit!=my_map->end(); ++rowit)
{
     int id       = rowit->first;
     auto& rowData = rowit->second;
}

Or even more to the point:

for(auto const& row : *my_map)
{
     int id       = row.first;
     auto& rowData = row.second;
}

Try to reduce on cruft so you don't get overwhelmed by the code :)


[1] some points in case:

  • boost::container::flat_map is a template, so your declaration cannot possibly be correct. I suspect you actually have

    tlsSHMMap* tls_temp_map;
    

    Why would you give us false code? That's unrelated?

  • In fact, is it my_map in your actual code? or tls_temp_map? Or tls_main_map (which you don't show, but is declared and never used...)?

sehe
  • 374,641
  • 47
  • 450
  • 633