这是导致 C2664 错误的代码片段:
无法将参数 1 从 'std::unique_ptr<Component,std::default_delete<_Ty>>' 转换为 'ComPtr &'
那么为什么非 const 引用必须用左值初始化呢?除了声明一个新变量外,如何避免这种情况?
#include <memory>
#include <list>
class Component {
};
using ComPtr = unique_ptr<Component>;
using ComList = list<ComPtr>;
ComList coms;
void addComponent(ComPtr&c) {
coms.push_back(c);
}
int main() {
addComponent(make_unique<Component>()); //Error here.
return 0;
}