problem with map construction

Discussion in 'C++' started by kap, Feb 15, 2006.

  1. kap

    kap Guest

    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.
     
    kap, Feb 15, 2006
    #1
    1. Advertising

  2. kap

    Mark P Guest

    kap wrote:
    > 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.
    >
     
    Mark P, Feb 15, 2006
    #2
    1. Advertising

  3. kap

    Mark P Guest

    Mark P wrote:
    > kap wrote:
    >> 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
     
    Mark P, Feb 15, 2006
    #3
  4. kap

    Kai-Uwe Bux Guest

    kap wrote:

    > 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
     
    Kai-Uwe Bux, Feb 15, 2006
    #4
  5. kap

    kap Guest

    hey Mark thanks a lot for the prompt reply and for pointing me to the
    faq.
    I highly appreciate that.
     
    kap, Feb 15, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alex
    Replies:
    1
    Views:
    690
    Lau Lei Cheong
    Feb 4, 2005
  2. steven
    Replies:
    9
    Views:
    3,273
    Chris Uppal
    Nov 12, 2003
  3. Matthias Hildebrand
    Replies:
    5
    Views:
    8,192
    krogers
    Mar 20, 2012
  4. Vlad
    Replies:
    0
    Views:
    388
  5. Ook
    Replies:
    10
    Views:
    579
Loading...

Share This Page