与C++ 11中的普通指针相比,智能指针的开销是多少?换句话说,如果我使用智能指针,我的代码会变慢吗?如果是这样,速度会慢多少?
具体来说,我问的是C++ 11 std::shared_ptr和std::unique_ptr.
显然,推下堆栈的东西会变得更大(至少我认为是这样),因为智能指针也需要存储其内部状态(引用计数等),问题是,这是多少影响我的表现,如果有的话?
例如,我从函数而不是普通指针返回一个智能指针:
std::shared_ptr<const Value> getValue();
// versus
const Value *getValue();
Run Code Online (Sandbox Code Playgroud)
或者,例如,当我的一个函数接受智能指针作为参数而不是普通指针时:
void setValue(std::shared_ptr<const Value> val);
// versus
void setValue(const Value *val);
Run Code Online (Sandbox Code Playgroud)