problem with map construction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kap

    #1

    problem with map construction

    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.

  • Mark P

    #2
    Re: problem with map construction

    kap wrote:[color=blue]
    > 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;
    > }[/color]

    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
    [color=blue]
    >
    > 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.
    >[/color]

    Comment

    • Mark P

      #3
      Re: problem with map construction

      Mark P wrote:[color=blue]
      > kap wrote:[color=green]
      >> 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;
      >> }[/color]
      >
      > 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
      >[/color]

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


      Comment

      • Kai-Uwe Bux

        #4
        Re: problem with map construction

        kap wrote:
        [color=blue]
        > 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.[/color]

        It's a FAQ:




        Best

        Kai-Uwe Bux

        Comment

        • kap

          #5
          Re: problem with map construction

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

          Comment

          Working...