【发布时间】:2020-09-12 00:15:37
【问题描述】:
template <typename Key, typename Resource>
class ResourceHolder {
std::unordered_map<Key, std::unique_ptr<Resource>> resources;
public:
Resource& get(const Key& key) const {
if (auto resource = resources.find(key); resource != std::end(resources)) {
return *(resource->second);
}
}
inline const Resource& operator[](Key&& key) const {
return get(std::forward<Key>(key));
}
};
我正在尝试学习移动语义,我想知道我在 operator[] 中对 std::forward 的使用 - 是否正确?
【问题讨论】:
标签: c++ c++11 move-semantics perfect-forwarding