clearing capacity af a vector

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

    clearing capacity af a vector

    We had a thread earlies about how to clear the capacity af a std::vector...
    how about this:

    std::vector<int > v;

    v.~vector<int>( ); // destruct it (don't know if I need
    the <int> here?)
    new (&v)vector<int> (); // construct it again

    --
    Lasse


  • Victor Bazarov

    #2
    Re: clearing capacity af a vector

    "Lasse Skyum" <no spam> wrote...[color=blue]
    > We had a thread earlies about how to clear the capacity af a[/color]
    std::vector...[color=blue]
    > how about this:
    >
    > std::vector<int > v;
    >
    > v.~vector<int>( ); // destruct it (don't know if I need
    > the <int> here?)
    > new (&v)vector<int> (); // construct it again[/color]

    While it _might_ work (and even according some twisted reading
    of the Standard, it is allowed to work), I'd suggest

    v.swap(std::vec tor<int>());

    Victor



    Comment

    • Chris Theis

      #3
      Re: clearing capacity af a vector


      "Lasse Skyum" <no spam> wrote in message
      news:3fa13435$0 $27467$edfadb0f @dread16.news.t ele.dk...[color=blue]
      > We had a thread earlies about how to clear the capacity af a[/color]
      std::vector...[color=blue]
      > how about this:
      >
      > std::vector<int > v;
      >
      > v.~vector<int>( ); // destruct it (don't know if I need
      > the <int> here?)
      > new (&v)vector<int> (); // construct it again
      >
      > --[/color]

      As Victor already said - this might work. But IMHO using placement new with
      any real need to is not such a hot idea. BTW what's the problem with using
      the "normal" solution with the swap function?

      Chris


      Comment

      • Andrew Koenig

        #4
        Re: clearing capacity af a vector

        > While it _might_ work (and even according some twisted reading[color=blue]
        > of the Standard, it is allowed to work), I'd suggest[/color]
        [color=blue]
        > v.swap(std::vec tor<int>());[/color]

        This technique almost works, but not quite. The trouble is that
        std::vector<int >() is an rvalue, which means that it can't be used as the
        argument to swap, which requires an lvalue.

        You can make the technique work with a slightly sneaky trick:

        std::vector<int >().swap(v);

        This code relies on the fact that it is permissible to call a member
        function that changes the contents of an rvalue.


        Comment

        • Victor Bazarov

          #5
          Re: clearing capacity af a vector

          "Andrew Koenig" <ark@acm.org> wrote...[color=blue][color=green]
          > > While it _might_ work (and even according some twisted reading
          > > of the Standard, it is allowed to work), I'd suggest[/color]
          >[color=green]
          > > v.swap(std::vec tor<int>());[/color]
          >
          > This technique almost works, but not quite. The trouble is that
          > std::vector<int >() is an rvalue, which means that it can't be used as the
          > argument to swap, which requires an lvalue.
          >
          > You can make the technique work with a slightly sneaky trick:
          >
          > std::vector<int >().swap(v);
          >
          > This code relies on the fact that it is permissible to call a member
          > function that changes the contents of an rvalue.[/color]

          Thank you for the correction. I ought to check before posting.

          Victor


          Comment

          • Lasse Skyum

            #6
            Re: clearing capacity af a vector


            [color=blue]
            > As Victor already said - this might work. But IMHO using placement new[/color]
            with[color=blue]
            > any real need to is not such a hot idea. BTW what's the problem with using
            > the "normal" solution with the swap function?[/color]

            The only reason was, that I was told that the swap method was not guarenteed
            to work by the standard. I thought this method would...

            I don't really understand why it wouldn't work... the resulting object
            should be a newly initialized vector. Just because it uses the same piece of
            memory that once was used for another vector should not affect that...or?

            Maybe it would be inefficient compared to the swap method? (Suppose it was
            something that happened verry often)

            --
            Lasse


            Comment

            • Victor Bazarov

              #7
              Re: clearing capacity af a vector

              "Lasse Skyum" <no spam> wrote...[color=blue]
              >
              >[color=green]
              > > As Victor already said - this might work. But IMHO using placement new[/color]
              > with[color=green]
              > > any real need to is not such a hot idea. BTW what's the problem with[/color][/color]
              using[color=blue][color=green]
              > > the "normal" solution with the swap function?[/color]
              >
              > The only reason was, that I was told that the swap method was not[/color]
              guarenteed[color=blue]
              > to work by the standard. I thought this method would...
              >
              > I don't really understand why it wouldn't work... the resulting object
              > should be a newly initialized vector. Just because it uses the same piece[/color]
              of[color=blue]
              > memory that once was used for another vector should not affect that...or?
              >
              > Maybe it would be inefficient compared to the swap method? (Suppose it was
              > something that happened verry often)[/color]

              No, the efficiency of those two methods is very close. Swapping
              variation is basically the same as constructing an empty one and
              destroying the original one, with the addition of swapping some
              data members, so swapping may actually be just a tad slower.

              Victor


              Comment

              • Lasse Skyum

                #8
                Re: clearing capacity af a vector

                [color=blue]
                > No, the efficiency of those two methods is very close. Swapping
                > variation is basically the same as constructing an empty one and
                > destroying the original one, with the addition of swapping some
                > data members, so swapping may actually be just a tad slower.
                >
                > Victor[/color]

                Okay, seems strange that people don't do it that way then... I still don't
                see why it wouldn't work?

                --
                Lasse


                Comment

                • Victor Bazarov

                  #9
                  Re: clearing capacity af a vector

                  "Lasse Skyum" <no spam> wrote...[color=blue]
                  >[color=green]
                  > > No, the efficiency of those two methods is very close. Swapping
                  > > variation is basically the same as constructing an empty one and
                  > > destroying the original one, with the addition of swapping some
                  > > data members, so swapping may actually be just a tad slower.
                  > >
                  > > Victor[/color]
                  >
                  > Okay, seems strange that people don't do it that way then... I still don't
                  > see why it wouldn't work?[/color]

                  Well, I don't want this to turn into another discussion why using
                  a destructor and then placement new isn't such a good idea, you
                  can find it on Google Groups, if you want to see what people think.
                  In short: if you have to call a destructor directly on something
                  you didn't create using placement new, you might need to rethink
                  your design or implementation. IMHO.

                  Victor



                  Comment

                  • Lasse Skyum

                    #10
                    Re: clearing capacity af a vector


                    [color=blue]
                    > In short: if you have to call a destructor directly on something
                    > you didn't create using placement new, you might need to rethink
                    > your design or implementation. IMHO.[/color]

                    Well that kind of makes sence... a little anyway :-)

                    --
                    Lasse


                    Comment

                    • Howard Hinnant

                      #11
                      Re: clearing capacity af a vector

                      In article <jDbob.47350$27 5.116409@attbi_ s53>,
                      "Victor Bazarov" <v.Abazarov@com Acast.net> wrote:
                      [color=blue]
                      > "Andrew Koenig" <ark@acm.org> wrote...[color=green][color=darkred]
                      > > > While it _might_ work (and even according some twisted reading
                      > > > of the Standard, it is allowed to work), I'd suggest[/color]
                      > >[color=darkred]
                      > > > v.swap(std::vec tor<int>());[/color]
                      > >
                      > > This technique almost works, but not quite. The trouble is that
                      > > std::vector<int >() is an rvalue, which means that it can't be used as the
                      > > argument to swap, which requires an lvalue.
                      > >
                      > > You can make the technique work with a slightly sneaky trick:
                      > >
                      > > std::vector<int >().swap(v);
                      > >
                      > > This code relies on the fact that it is permissible to call a member
                      > > function that changes the contents of an rvalue.[/color]
                      >
                      > Thank you for the correction. I ought to check before posting.[/color]

                      That's a common mistake, even for experts such as yourself. For that
                      reason, I'd love to see:

                      template <class T>
                      void
                      swap(T&&, T&&);

                      (see move proposal:

                      )

                      which would allow expressions such as:

                      swap(v, std::vector<int >());

                      or:

                      swap(std::vecto r<int>(), v);

                      which I've personally grown to like (ymmv).

                      -Howard

                      Comment

                      Working...