vendredi 13 août 2021

Why can't emplace accept begin and end as parameter

I'm developing a basic(low-level) c++11 library for my team. Now I'm trying to develop a custom container.

template<typename T1, typename T2>
class CustomMap {
public:
    void insert(const std::map<T1, T2>& container) { mp_.insert(container.begin(), container.end()); }
    void emplace(std::map<T1, T2>&& container) { mp_.emplace(container.begin(), container.end()); }

private:
    std::map<T1, T2> mp_;
};

int main() {
    CustomMap<int, int> mp;
    std::map<int, int> mm;
    mm[1] = 2;
    mp.emplace(std::move(mm)); // ERROR

    return 0;
}

It doesn't seem that std::map::emplace can accept two parameters: begin and end?

So why can std::map::insert accept begin and end but std::map::emplace can't?

In the function void emplace of my code, I have to use a loop?

for (auto && ele : container) {
    mp_.emplace(ele);
}

Aucun commentaire:

Enregistrer un commentaire