Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14443 > unrolled thread

A question on "uncopyable" that disable copy constructor

Started byTH <python152@gmail.com>
First post2012-02-22 20:45 -0800
Last post2012-02-28 17:01 -0800
Articles 6 on this page of 26 — 10 participants

Back to article view | Back to comp.lang.c++


Contents

  A question on "uncopyable" that disable copy constructor TH <python152@gmail.com> - 2012-02-22 20:45 -0800
    Re: A question on "uncopyable" that disable copy constructor Ian Collins <ian-news@hotmail.com> - 2012-02-23 18:33 +1300
      Re: A question on "uncopyable" that disable copy constructor Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-23 08:47 +0000
        Re: A question on "uncopyable" that disable copy constructor Ian Collins <ian-news@hotmail.com> - 2012-02-26 08:16 +1300
          Re: A question on "uncopyable" that disable copy constructor Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-25 19:49 +0000
            Re: A question on "uncopyable" that disable copy constructor yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-02-27 14:32 +0000
              Re: A question on "uncopyable" that disable copy constructor Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-27 15:54 +0000
                Re: A question on "uncopyable" that disable copy constructor yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-02-27 16:15 +0000
                  Re: A question on "uncopyable" that disable copy constructor Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-27 20:58 +0000
                    Re: A question on "uncopyable" that disable copy constructor Ian Collins <ian-news@hotmail.com> - 2012-02-28 17:18 +1300
                      Re: A question on "uncopyable" that disable copy constructor Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-02-28 00:00 -0500
                        Re: A question on "uncopyable" that disable copy constructor Ian Collins <ian-news@hotmail.com> - 2012-02-28 20:09 +1300
                        Re: A question on "uncopyable" that disable copy constructor Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-28 21:19 +0000
                          Re: A question on "uncopyable" that disable copy constructor Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-03-04 01:05 -0500
                            Re: A question on "uncopyable" that disable copy constructor 88888 Dihedral <dihedral88888@googlemail.com> - 2012-03-04 18:01 -0800
                      Re: A question on "uncopyable" that disable copy constructor Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-28 22:02 +0000
                        Re: A question on "uncopyable" that disable copy constructor Ian Collins <ian-news@hotmail.com> - 2012-02-29 11:15 +1300
    Re: A question on "uncopyable" that disable copy constructor Miles Bader <miles@gnu.org> - 2012-02-23 15:14 +0900
      Re: A question on "uncopyable" that disable copy constructor "T.H." <python152@gmail.com> - 2012-02-23 01:51 -0800
    Re: A question on "uncopyable" that disable copy constructor bartek szurgot <basz@no.spam> - 2012-02-24 17:18 +0100
      Re: A question on "uncopyable" that disable copy constructor Juha Nieminen <nospam@thanks.invalid> - 2012-02-25 19:10 +0000
        Re: A question on "uncopyable" that disable copy constructor Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-02-25 17:45 -0500
          Re: A question on "uncopyable" that disable copy constructor Juha Nieminen <nospam@thanks.invalid> - 2012-02-26 09:38 +0000
            Re: A question on "uncopyable" that disable copy constructor Juha Nieminen <nospam@thanks.invalid> - 2012-02-26 09:42 +0000
            Re: A question on "uncopyable" that disable copy constructor Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-02-27 23:23 -0500
        Re: A question on "uncopyable" that disable copy constructor 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-28 17:01 -0800

Page 2 of 2 — ← Prev page 1 [2]


#14498

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-02-25 19:10 +0000
Message-ID<4f493210$0$3079$7b1e8fa0@news.nbl.fi>
In reply to#14486
bartek szurgot <basz@no.spam> wrote:
> std::vector<Movable> v;
> Movable m;
> v.push_back(m); // error - cannot copy
> v.push_back( std::move(m) ); // ok - moves object

  I'm not so sure doing that is such a good "pattern" because after
giving 'm' to the vector you are left with an "empty"/"null" 'm' which
has nothing in it (because what it had was moved to the vector).

  A more sensible "pattern" would be to create a temporary that gets
destroyed immediately after its contents have been moved to the vector,
that is:

    v.push_back(Movable());

  OTOH, there might be some situations where you need to do it like your
version, for example like:

    Movable m;
    m.doSomething(abc);
    m.doSomethingElse(def);
    v.push_back(std::move(m));

  However, in this case it might be worthy of consideration to move such
initialization to a function, so you would have something like:

    Movable gimmeAMovable()
    {
        Movable m;
        m.doSomething(abc);
        m.doSomethingElse(def);
        return m;
    }
    ...
    v.push_back(gimmeAMovable());

  This way you don't end up with an "empty" object from which content has
been moved away.

[toc] | [prev] | [next] | [standalone]


#14501

FromPavel <pauldontspamtolk@removeyourself.dontspam.yahoo>
Date2012-02-25 17:45 -0500
Message-ID<4f496477$0$40584$c3e8da3$3a1a2348@news.astraweb.com>
In reply to#14498
Juha Nieminen wrote:
> bartek szurgot<basz@no.spam>  wrote:
>> std::vector<Movable>  v;
>> Movable m;
>> v.push_back(m); // error - cannot copy
>> v.push_back( std::move(m) ); // ok - moves object
>
>    I'm not so sure doing that is such a good "pattern" because after
> giving 'm' to the vector you are left with an "empty"/"null" 'm' which
> has nothing in it (because what it had was moved to the vector).
>
>    A more sensible "pattern" would be to create a temporary that gets
> destroyed immediately after its contents have been moved to the vector,
> that is:
>
>      v.push_back(Movable());
They've got this, too, in C++11 (some useful new stuff for a change):

v.emplace(v.end(), EmplaceConstructible());
...

HTH
-Pavel

[toc] | [prev] | [next] | [standalone]


#14507

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-02-26 09:38 +0000
Message-ID<4f49fd7d$0$4385$7b1e8fa0@news.nbl.fi>
In reply to#14501
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> wrote:
> v.emplace(v.end(), EmplaceConstructible());

  Doesn't that call the copy constructor of EmplaceConstructible (which
kind of defeats the whole idea of having a non-copyable class)?

  If I understand the documentation correctly, if you wanted to insert
a default-constructed element you would do it like:

    v.emplace(v.end());

and if you wanted to give the constructor parameters, you simply give
them as parameters to emplace(), like:

    v.emplace(v.end(), 1, 2, 3);

  (Perfect forwarding rulez.)

[toc] | [prev] | [next] | [standalone]


#14509

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-02-26 09:42 +0000
Message-ID<4f49fe78$0$2835$7b1e8fa0@news.nbl.fi>
In reply to#14507
Juha Nieminen <nospam@thanks.invalid> wrote:
>    v.emplace(v.end());

  Of, if you are adding it to the end anyways, then:

    v.emplace_back();

[toc] | [prev] | [next] | [standalone]


#14571

FromPavel <pauldontspamtolk@removeyourself.dontspam.yahoo>
Date2012-02-27 23:23 -0500
Message-ID<4f4c56bb$0$2598$c3e8da3$f5af001f@news.astraweb.com>
In reply to#14507
Juha Nieminen wrote:
> Pavel<pauldontspamtolk@removeyourself.dontspam.yahoo>  wrote:
>> v.emplace(v.end(), EmplaceConstructible());
>
>    Doesn't that call the copy constructor of EmplaceConstructible (which
> kind of defeats the whole idea of having a non-copyable class)?
True, that was the wrong syntax.

>
>    If I understand the documentation correctly, if you wanted to insert
> a default-constructed element you would do it like:
>
>      v.emplace(v.end());
>
> and if you wanted to give the constructor parameters, you simply give
> them as parameters to emplace(), like:
>
>      v.emplace(v.end(), 1, 2, 3);
>
>    (Perfect forwarding rulez.)

-Pavel

[toc] | [prev] | [next] | [standalone]


#14604

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-02-28 17:01 -0800
Message-ID<23937421.477.1330477265253.JavaMail.geo-discussion-forums@pbsp8>
In reply to#14498
在 2012年2月26日星期日UTC+8上午3时10分08秒,Juha Nieminen写道:
> bartek szurgot <basz@no.spam> wrote:
> > std::vector<Movable> v;
> > Movable m;
> > v.push_back(m); // error - cannot copy
> > v.push_back( std::move(m) ); // ok - moves object
> 
>   I'm not so sure doing that is such a good "pattern" because after
> giving 'm' to the vector you are left with an "empty"/"null" 'm' which
> has nothing in it (because what it had was moved to the vector).
> 
>   A more sensible "pattern" would be to create a temporary that gets
> destroyed immediately after its contents have been moved to the vector,
> that is:
> 
>     v.push_back(Movable());
> 
>   OTOH, there might be some situations where you need to do it like your
> version, for example like:
> 
>     Movable m;
>     m.doSomething(abc);
>     m.doSomethingElse(def);
>     v.push_back(std::move(m));
> 
>   However, in this case it might be worthy of consideration to move such
> initialization to a function, so you would have something like:
> 
>     Movable gimmeAMovable()
>     {
>         Movable m;
>         m.doSomething(abc);
>         m.doSomethingElse(def);
>         return m;
>     }
>     ...
>     v.push_back(gimmeAMovable());
> 
>   This way you don't end up with an "empty" object from which content has
> been moved away.



在 2012年2月26日星期日UTC+8上午3时10分08秒,Juha Nieminen写道:
> bartek szurgot <basz@no.spam> wrote:
> > std::vector<Movable> v;
> > Movable m;
> > v.push_back(m); // error - cannot copy
> > v.push_back( std::move(m) ); // ok - moves object
> 
>   I'm not so sure doing that is such a good "pattern" because after
> giving 'm' to the vector you are left with an "empty"/"null" 'm' which
> has nothing in it (because what it had was moved to the vector).
> 
>   A more sensible "pattern" would be to create a temporary that gets
> destroyed immediately after its contents have been moved to the vector,
> that is:
> 
>     v.push_back(Movable());
> 
>   OTOH, there might be some situations where you need to do it like your
> version, for example like:
> 
>     Movable m;
>     m.doSomething(abc);
>     m.doSomethingElse(def);
>     v.push_back(std::move(m));

I  think here the index of the  moved  m in v should be saved in the next line. But it is possible that  the vector v is used as a stack of the same type of objects.


[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c++


csiph-web