problem with map construction

K

kap

Hi all,
I am facing a weired problem when I try using maps. Here is the
sample code.
Can anyone please tell me where am I going wrong.

int main() {
less<int> ltvar;
std::map<int, int> m2(less<int>());
m2[1]; // compilation fails at this point
return 0;
}

whereas this particular code works fine
int main() {
less<int> ltvar;
std::map<int, int> m2( ltvar ); // less<int>() changed to ltvar
m2[1]; // compilation succeeds
return 0;
}

I would appreciate if someone can comment on what the issue is.
 
M

Mark P

kap said:
Hi all,
I am facing a weired problem when I try using maps. Here is the
sample code.
Can anyone please tell me where am I going wrong.

int main() {
less<int> ltvar;
std::map<int, int> m2(less<int>());
m2[1]; // compilation fails at this point
return 0;
}

I'm not an expert but it seems, from looking at VC++EE compiler messages
(btw, always post compiler messages if you can't compile), that this is
interpreted as a _declaration of a function_ which takes a ptr to
function as an argument and returns a map.

You can get around this like so:

std::map<int, int> m2 = std::map<int, int> (less<int>());

However, there's no reason why you need to do any of this since
less<Key> is the default comparison function used by map anyway.

-Mark
whereas this particular code works fine
int main() {
less<int> ltvar;
std::map<int, int> m2( ltvar ); // less<int>() changed to ltvar
m2[1]; // compilation succeeds
return 0;
}

I would appreciate if someone can comment on what the issue is.
 
M

Mark P

Mark said:
kap said:
Hi all,
I am facing a weired problem when I try using maps. Here is the
sample code.
Can anyone please tell me where am I going wrong.

int main() {
less<int> ltvar;
std::map<int, int> m2(less<int>());
m2[1]; // compilation fails at this point
return 0;
}

I'm not an expert but it seems, from looking at VC++EE compiler messages
(btw, always post compiler messages if you can't compile), that this is
interpreted as a _declaration of a function_ which takes a ptr to
function as an argument and returns a map.

You can get around this like so:

std::map<int, int> m2 = std::map<int, int> (less<int>());

However, there's no reason why you need to do any of this since
less<Key> is the default comparison function used by map anyway.

-Mark

I knew I'd seen this in the FAQ before...

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
 
K

Kai-Uwe Bux

kap said:
Hi all,
I am facing a weired problem when I try using maps. Here is the
sample code.
Can anyone please tell me where am I going wrong.

int main() {
less<int> ltvar;
std::map<int, int> m2(less<int>());
m2[1]; // compilation fails at this point
return 0;
}

whereas this particular code works fine
int main() {
less<int> ltvar;
std::map<int, int> m2( ltvar ); // less<int>() changed to ltvar
m2[1]; // compilation succeeds
return 0;
}

I would appreciate if someone can comment on what the issue is.

It's a FAQ:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19


Best

Kai-Uwe Bux
 
K

kap

hey Mark thanks a lot for the prompt reply and for pointing me to the
faq.
I highly appreciate that.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top