Home > Enterprise >  Create a `map` using `unique_ptr`
Create a `map` using `unique_ptr`

Time:06-12

I originally had a problem creating a map of classes, with some help I realized I actually need a map<string, unique_ptr<myclass>>.

To be more precise:

  • I have a bunch of specialized classes with a common ancestor.
  • specialized classes are actually specialized from the same template.
  • I have a std::map<std::string, unique_ptr<ancestor>> taking ownership of my instances (I hope).
  • I need ownership because the function creating the instances is in a completely different section of code than the place using them.
  • If useful: I create the map at initialization time and then I only reference it at runtime; for all practical purposes after initializing it (a complex affair, involving reading config files) it could become const.

A minimal example of what I need to achieve is:

#include <iostream>
#include <string>
#include <memory>
#include <map>

class generic {
    std::string _name;

public:
    generic(std::string name) : _name(name) {}
    virtual ~generic() = default;

    virtual std::string name() { return _name; }
    virtual std::string value() { return "no value in generic"; }
};

template <class T> class special : public generic {
    T _value;
public:
    special(std::string name, T value) : generic(name), _value(value) {}
    virtual ~special() = default;

    std::string value() override { return std::to_string(_value); }
};

template <typename T> void add_item(std::map <std::string, std::unique_ptr<generic>> &m, const std::string &n, const T &v) {
    m[n] = std::make_unique<special<T>>(typeid(v).name(), v);
}

int
main() {
    std::map <std::string, std::unique_ptr<generic>> instances;

    add_item<int>(instances, "int", 1);
    add_item<bool>(instances, "bool", true);
    add_item<float>(instances, "float", 3.1415);

    for (auto i : instances) {
        std::cout << i.first << " -- " << i.second.get()->name() << " -- " << i.second.get()->value() << std::endl;
    }
    return 0;
}

Unfortunately I seem to be missing something because compilation bombs with "error: use of deleted function".

Can someone be so kind to help me sort this out?

CodePudding user response:

In this loop you try to copy unique_ptrs, but the unique_ptr copy constructor is deleted.

for (auto i : instances) {

You need to take them by reference instead:

for (auto& i : instances) {
  • Related