Optimizing the number of constructor calls

后端 未结 2 2032
傲寒
傲寒 2021-02-19 13:04

At work we have a class with an expensive constructor so we would like it to be called as few times as possible. We looked through the uses of it and tried to make the code more

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-19 13:30

    If the compiler cannot inline append, then it cannot determine that the return value is target object. Then it doesn't know that the temporary is being returned, and cannot construct it in place.

    You would have the same behavior with:

    Imaginary tmp(*this);
    return tmp.append(rhs);
    

    If the return value of append is opaque to the compiler (defined in another compilation unit), it prevents the optimization.

提交回复
热议问题