Questions about postblit and move semantics

前端 未结 2 1553
孤街浪徒
孤街浪徒 2020-12-31 06:38

I have already asked a similar question a while ago, but I\'m still unclear on some details.

  1. Under what circumstances is the postblit constructor called?

2条回答
  •  别那么骄傲
    2020-12-31 07:00

    As far as I understand:

    1) When a struct is copied, as opposed to moved or constructed.

    2) The point of move semantics is that neither of the two needs to happen. The new location of the struct is initialized with a bit-wise copy of the struct, and the old location goes out of scope and becomes inaccessible. Thus, the struct has "moved" from A to B.

    3) That is the typical move situation:

    S init(bool someFlag)
    {
        S s;
        s.foo = someFlag? bar : baz;
        return s; // `s` can now be safely moved from here...
    }
    
    // call-site:
    S s = init(flag);
    //^ ... to here.
    

提交回复
热议问题