lundi 25 mai 2020

Perfect forwarding in operator[]

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));
    }
};

I am trying to learn move semantics and I am wondering about my usage of std::forward inside operator[] - is it correct?

Aucun commentaire:

Enregistrer un commentaire