Anurag Peshne Anurag Peshne - 2 months ago 16
C++ Question

C++ nested map no matching member function const member

I have a nested map, something like

map = {
key : {
innerKey: innerVal
}
}


I'm trying to search for
innerVal
from member function marked as
const
. I'm using
at()
as described here C++ map access discards qualifiers (const)
which gets me to map pointed by
key
. But, when I try to use
at()
on nested map, I get an error:

error: no matching member function for call to 'at'


Workaround: I can use a iterator and linearly search on the nested map, which works perfectly. How can I use functions such as
at()
or
find()
to search in nested map.

TLDR:

private std::map<int, std::map<int, int> > privateStore;

int search(int key1, int key2) const {
return privateStore.at(key1).at(key2); //works when I remove `const` from function signature

}


Edit: it works for above simplified code, try this, and try removing
const
keyword from line 20.

#include <iostream>
#include <map>
#include <thread>

template <typename T>
class Foo
{
public:
Foo()
{
std::cout << "init";
}

void set(T val)
{
privateStore[std::this_thread::get_id()][this] = val;
}

T search(std::thread::id key1) const
{
std::map<Foo<T>*, T> retVal = privateStore.at(key1); //works when I remove `const` from function signature
return retVal.at(this);
}

private:
static std::map<std::thread::id, std::map<Foo<T>*, T> > privateStore;
};

template<typename T> std::map<std::thread::id, std::map<Foo<T>*, T> > Foo<T>::privateStore = {};

int main()
{
Foo<int> a;
a.set(12);
std::cout << a.search(std::this_thread::get_id());
}

Answer

Declare your inner map's key to be a pointer to const object. Otherwise when you pass this in a const function you pass Foo<T> const* instead of Foo<T>* and you can't convert that implicitly.

So

static std::map<std::thread::id, std::map<Foo<T> *, T> > privateStore;

to

static std::map<std::thread::id, std::map<Foo<T> const*, T> > privateStore;

And the same in the definition.

live example of your example - fixed.