double assignment

Discussion in 'C++' started by Triple-DES, Jun 12, 2008.

  1. Triple-DES

    Triple-DES Guest

    Consider the following code:

    struct C {
    C(int i) : j(i) {}
    int j;
    };

    int main() {
    C c1(42);
    C c = c = c1;
    }

    Does it have well-defined behaviour?

    DP
    Triple-DES, Jun 12, 2008
    #1
    1. Advertising

  2. Triple-DES

    Triple-DES Guest

    On 12 Jun, 09:56, "Fred Zwarts" <> wrote:
    > "Triple-DES" <> wrote:
    > > int main() {
    > >  C c1(42);
    > >  C c = c = c1;
    > > }


    > I think it is not a double assignment. It is initialization and assignment..
    > C c = c = c1; can be rewritten as C c (c = c1),
    > which makes clear that c is used as a parameter for its own construction.


    Yes, a more suitable topic would be: copy-initialization and
    assignment. It seems obvious that C c = c; is undefined. However I am
    still not convinced about C c = (c = c1).

    DP
    Triple-DES, Jun 12, 2008
    #2
    1. Advertising

  3. Triple-DES <> writes:

    > Consider the following code:
    >
    > struct C {
    > C(int i) : j(i) {}
    > int j;
    > };
    >
    > int main() {
    > C c1(42);
    > C c = c = c1;
    > }
    >
    > Does it have well-defined behaviour?


    AFAIK, in your specific case, yes. However, the copy constructor
    (implicit in this case) may be called as many times as you assign to
    c. So if you had your own copy constructor, you would have to be
    careful about side effects and order of member copying, etc.


    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Jun 12, 2008
    #3
  4. Triple-DES

    James Kanze Guest

    On Jun 12, 9:23 am, Triple-DES <> wrote:
    > Consider the following code:


    > struct C {
    > C(int i) : j(i) {}
    > int j;
    > };


    > int main() {
    > C c1(42);
    > C c = c = c1;
    > }


    > Does it have well-defined behaviour?


    Formally, I don't think so, since you're calling C::eek:perator=
    before having constructed C (and the constructor is not
    trivial).

    In practice, I don't think it matters, since I don't think you'd
    ever want to do this anyway.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 12, 2008
    #4
  5. Triple-DES

    Daniel Pitts Guest

    Triple-DES wrote:
    > Consider the following code:
    >
    > struct C {
    > C(int i) : j(i) {}
    > int j;
    > };
    >
    > int main() {
    > C c1(42);
    > C c = c = c1;
    > }
    >
    > Does it have well-defined behaviour?
    >
    > DP

    The real question is,
    WHY!?!!?
    It seems likely to be a programmer error, and the meaning is very much
    *not* intuitive. If you have to spend that much time worrying about
    whether its well-defined, and what its defined behavior is, then you
    should consider a different idiom that is more intuitive for human readers.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jun 12, 2008
    #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. Otto Wyss
    Replies:
    2
    Views:
    1,989
    Howard
    Aug 19, 2003
  2. Sydex
    Replies:
    12
    Views:
    6,452
    Victor Bazarov
    Feb 17, 2005
  3. nagy
    Replies:
    36
    Views:
    979
    Terry Reedy
    Jul 20, 2006
  4. Chris
    Replies:
    34
    Views:
    1,480
  5. ded
    Replies:
    4
    Views:
    103
    Austin Ziegler
    Sep 4, 2005
Loading...

Share This Page