vector.push_back rvalue and copy-elision

前端 未结 2 700
轮回少年
轮回少年 2020-12-20 15:53

I push_back a temporary object into a vector like this,

vector vec;
vec.push_back(A(\"abc\"));

will the

2条回答
  •  一向
    一向 (楼主)
    2020-12-20 16:15

    In the general case, it cannot be done, it could potentially be done here as vector is a template and the code might be inlined, giving more information to the optimizer to do it's job and relieving some of the requirements of function calls.

    In the general case, copy elision works by placing the two objects over the same location in memory and having just two names to refer to a single object. The problem in this case would be that one of the arguments must be located inside the vector (dynamically allocated, at a particular position), and the other is an argument to the function, which might be bound by the calling convention to a particular position in the stack. If that is the case, then the compiler will not be able to optimize the copy.

提交回复
热议问题