std::map const troubles

G

Grey Plastic

I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
};

The error message states that I am discarding qualifiers by passing a
const map<...> to the map::find function. But there is a const
version of the map::find function. Why isn't the compiler using that
version?

If I replace "map<Foo*,int>" with "map<const Foo*,int>" or if I change
"lookup(const Foo& f)" with "lookup(Foo& f)" suddenly it works. Why?
I am befuddled.

How do I get this to work, without changing the signatures?
 
G

Guest

I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
};
lookup it supposed to return an int and I dont see it returning one.

Raj
 
J

John Harrison

Grey Plastic said:
I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }

I assume you mean something like this

int lookup(const Foo& f) const { return myMap.find(&f)->second; }
};

The error message states that I am discarding qualifiers by passing a
const map<...> to the map::find function. But there is a const
version of the map::find function. Why isn't the compiler using that
version?

It is using the const version, but the const version takes a Foo* parameter
and you are giving it a const Foo* parameter.
If I replace "map<Foo*,int>" with "map<const Foo*,int>" or if I change
"lookup(const Foo& f)" with "lookup(Foo& f)" suddenly it works. Why?

See above.
I am befuddled.

How do I get this to work, without changing the signatures?

int lookup(Foo f) const { return myMap.find(&f)->second; }

That works, at the cost of copying a Foo object.

john
 
R

Rob Williscroft

Grey Plastic wrote in
in comp.lang.c++:
I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
};

The error message states that I am discarding qualifiers by passing a
const map<...> to the map::find function. But there is a const
version of the map::find function. Why isn't the compiler using that
version?

If I replace "map<Foo*,int>" with "map<const Foo*,int>" or if I change
"lookup(const Foo& f)" with "lookup(Foo& f)" suddenly it works. Why?
I am befuddled.

How do I get this to work, without changing the signatures?

The key type of the map is `Foo * const` your lookup() function passes
in a `Foo const *` this cast *requires* const_cast<>, as even though
you are adding a const qualifier, you are also removing one.

IOW:

Foo const * -> Foo const * const, is ok but you are doing
Foo const * -> Foo * -> Foo * const.

The good news is that since std::map doesn't dereference the
'key' you can safely use const_cast<>().


int lookup( Foo const &f )
{
myMap.find( const_cast< Foo * >( &f ) );
}

HTH.

Rob.
 
J

John Harrison

int lookup(Foo f) const { return myMap.find(&f)->second; }

That works, at the cost of copying a Foo object.

It's rubbish. It compiles but it won't work because the address of the
object passed to find will be different from the address of the object used
to call lookup. Apologies.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top