std::map

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

    #16
    Re: std::map


    "Duane Hebert" <spoo@flarn.com > wrote in message
    news:nsH6b.1873 $dB3.59674@webe r.videotron.net ...[color=blue][color=green]
    > >
    > > So you've figured out that you need to insert pairs, so the thing in the[/color]
    > map[color=green]
    > > that the iterator is pointing to is a pair. You also said you want only[/color]
    > the[color=green]
    > > string, not the whole pair. *it would be the whole pair. In a pair,[/color]
    > there[color=green]
    > > is a "first" and "second". You want to point to the "second".[/color]
    >
    > Right but *it.second doesn't work.[/color]

    So?
    [color=blue]
    > One solution would be to do it->first since it returns a reference.
    > I don't like this because I think that it obscures the fact that *it is a
    > reference.
    > The solution that I was looking for was simply (*it).second.[/color]

    Yuck. Why would you want to muck it up like that? it->second is perfectly
    natural. I think it's crazy to say pointer notation "obscures" the fact
    that the dereferenced pointer is a reference. That's like saying you prefer
    the statement "It's false that I don't like pie" to the statement "I like
    pie." because the second sentence obscures the fact that you don't dislike
    it.


    Comment

    • llewelly

      #17
      Re: std::map

      "Smeckler" <smNOecklers@ho tSPAMmail.com> writes:
      [snip][color=blue]
      > Easy. And, since this is the STL we're talking about, why not create a
      > handy template function :)
      >
      > template <typename TypeName> TypeName& GetNullReferenc e()
      > {
      > return *(TypeName*)NUL L;
      > }[/color]

      This has undefined behavior.

      Comment

      • Kevin Goodsell

        #18
        Re: std::map

        Smeckler wrote:
        [color=blue]
        >
        > Easy. And, since this is the STL we're talking about, why not create a
        > handy template function :)
        >
        > template <typename TypeName> TypeName& GetNullReferenc e()
        > {
        > return *(TypeName*)NUL L;
        > }
        >
        >[/color]

        I hope you are joking. If not, you need to learn some C++.

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • Duane Hebert

          #19
          Re: std::map

          > Yuck. Why would you want to muck it up like that? it->second is
          perfectly[color=blue]
          > natural. I think it's crazy to say pointer notation "obscures" the fact
          > that the dereferenced pointer is a reference. That's like saying you[/color]
          prefer[color=blue]
          > the statement "It's false that I don't like pie" to the statement "I like
          > pie." because the second sentence obscures the fact that you don't dislike
          > it.[/color]

          I prefer to use the dot operator when referencing an object that's not a
          pointer. It's
          a matter of taste and not worth an argument. Both ways work. To me, it
          wasn't intuitive
          that (*it).first would work. But to answer your simile, I guess that it's
          not true that I do
          agree with you :-)


          Comment

          • Smeckler

            #20
            Re: std::map

            > > Easy. And, since this is the STL we're talking about, why not create a[color=blue][color=green]
            > > handy template function :)
            > >
            > > template <typename TypeName> TypeName& GetNullReferenc e()
            > > {
            > > return *(TypeName*)NUL L;
            > > }
            > >
            > >[/color]
            >
            > I hope you are joking. If not, you need to learn some C++.
            >
            > -Kevin[/color]


            Kevin,

            Would you care to be more specific about your objection to my code?




            Comment

            • Christoph Rabel

              #21
              Re: std::map

              Smeckler wrote:[color=blue]
              > Would you care to be more specific about your objection to my code?[/color]

              First, this has undefined behaviour. (Dereferencing a 0 Pointer)

              Second, how do you test the returned reference for 0??
              You cannot. So somebody ends up with a dangling reference. And probably
              crashes a while later when he tries to use it...

              Christoph

              Comment

              • tom_usenet

                #22
                Re: std::map

                On Tue, 9 Sep 2003 10:20:27 +0100, "Smeckler"
                <smNOecklers@ho tSPAMmail.com> wrote:
                [color=blue][color=green][color=darkred]
                >> > Easy. And, since this is the STL we're talking about, why not create a
                >> > handy template function :)
                >> >
                >> > template <typename TypeName> TypeName& GetNullReferenc e()
                >> > {
                >> > return *(TypeName*)NUL L;
                >> > }
                >> >
                >> >[/color]
                >>
                >> I hope you are joking. If not, you need to learn some C++.
                >>
                >> -Kevin[/color]
                >
                >
                >Kevin,
                >
                >Would you care to be more specific about your objection to my code?[/color]

                It has undefined behaviour. Dereferencing a null pointer is always a
                bug. In other words, in the language spec, there is no such thing as a
                null reference, since in order to create one, you have to go outside
                the language spec.

                In practice, the code will crash on some platforms, while on others it
                will return what you think of as a "null reference".

                Tom

                Comment

                • jeffc

                  #23
                  Re: std::map


                  "Duane Hebert" <spoo@flarn.com > wrote in message
                  news:I_77b.3909 $yg2.192615@web er.videotron.ne t...[color=blue]
                  >
                  > I prefer to use the dot operator when referencing an object that's not a
                  > pointer. It's
                  > a matter of taste and not worth an argument. Both ways work. To me, it
                  > wasn't intuitive
                  > that (*it).first would work.[/color]

                  The whole point is it's supposed to look like a pointer. Otherwise, you
                  wouldn't have to go around your elbow to get to your butt to dereference it,
                  thereby forcing it to look like a reference just because that's how you want
                  it to look.


                  Comment

                  • jeffc

                    #24
                    Re: std::map


                    "Duane Hebert" <spoo@flarn.com > wrote in message
                    news:I_77b.3909 $yg2.192615@web er.videotron.ne t...[color=blue]
                    >
                    > I prefer to use the dot operator when referencing an object that's not a
                    > pointer. It's
                    > a matter of taste and not worth an argument. Both ways work. To me, it
                    > wasn't intuitive
                    > that (*it).first would work.[/color]

                    Or to put it another way, if it were just a regular reference object, you
                    would access it like
                    it.
                    instead of like
                    (*it).
                    So it's not just a matter of preference in this case, it's a matte of how
                    the thing was designed to be used - i.e., like a pointer.


                    Comment

                    • Kevin Goodsell

                      #25
                      Re: std::map

                      Smeckler wrote:[color=blue][color=green][color=darkred]
                      >>>Easy. And, since this is the STL we're talking about, why not create a
                      >>>handy template function :)
                      >>>
                      >>>template <typename TypeName> TypeName& GetNullReferenc e()
                      >>>{
                      >>> return *(TypeName*)NUL L;
                      >>>}
                      >>>
                      >>>[/color]
                      >>
                      >>I hope you are joking. If not, you need to learn some C++.
                      >>
                      >>-Kevin[/color]
                      >
                      >
                      >
                      > Kevin,
                      >
                      > Would you care to be more specific about your objection to my code?
                      >[/color]

                      I could, but I think I'll let Herb Sutter do it instead:



                      To summarize: You cannot have a null reference. You cannot dereference a
                      null pointer, under any circumstances.

                      -Kevin
                      --
                      My email address is valid, but changes periodically.
                      To contact me please use the address from a recent posting.

                      Comment

                      Working...