简体   繁体   English

std::map emplace 不可移动不可复制非默认可构造类型

[英]std::map emplace non-movable non-copyable non-default-constructible type

I want a map of a class that has no copy ctor, no move ctor, and no default ctor (I have no control over this class).我想要一个 class 的 map,它没有复制 ctor、移动 ctor 和默认 ctor(我无法控制此类)。

I've tried using std::map::emplace with the std::piecewise_construct and std::forward_as_tuple arguments, but it seems like the compiler is telling me that this is not possible because it tries default constructing first.我已经尝试使用 std::map::emplace 与 std::piecewise_construct 和 std::forward_as_tuple arguments,但似乎编译器告诉我这是不可能的,因为它首先尝试默认构造。

// Example program
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <tuple>

class stone
{
public:
    stone() = delete;
    stone(stone& s) = delete;
    stone(stone&& s) = delete;
    stone(const std::string& s) : str(s)
    {
    }
    std::string str;
};

int main()
{
    std::map<int, stone> m;
    m.emplace(std::piecewise_construct, std::forward_as_tuple(5), std::forward_as_tuple("asdf"));
    std::cout << "map[5]: " << m[5].str << "\n";
}

See the example with the compiler error here: http://cpp.sh/8bbwh在此处查看带有编译器错误的示例: http://cpp.sh/8bbwh

How can I make this work?我怎样才能使这项工作? I've tried looking at similar questions, but they don't seem to contain any answers that are useful to my very specific scenario.我试过查看类似的问题,但它们似乎不包含任何对我非常具体的场景有用的答案。

std::map emplace non-movable non-copyable non-default-constructible type std::map emplace 不可移动不可复制非默认可构造类型

Like this:像这样:

m.emplace(5, "asdf");

There is no need for std::piecewise_construct nor std::forward_as_tuple.不需要 std::piecewise_construct 也不需要 std::forward_as_tuple。


As for looking up the value, you cannot use the subscript operator because that requires the type to be default constructible - the default construction will be used when the value isn't in the map before lookup.至于查找值,您不能使用下标运算符,因为这要求类型是默认可构造的 - 当查找前 map 中的值不存在时,将使用默认构造。

To lookup a non-default constructible type, you must use map::find or map::at instead.要查找非默认可构造类型,您必须使用 map::find 或 map::at 代替。 Former returns iterator to end and latter throws an exception if the mapped value does not exist.前者将迭代器返回到结束,后者如果映射的值不存在则抛出异常。

There's no issue with the emplace operation, however, the issue is with std::map::operator[] since it requires the value_type to be default constructible. emplace 操作没有问题,但是,问题在于std::map::operator[]因为它要求value_type是默认可构造的。 However, stone has a deleted default constructor.但是, stone有一个已删除的默认构造函数。

Instead, you could use std::map::at which does not have that requirement:相反,您可以使用没有该要求std::map::at

std::cout << "map[5]: " << m.at(5).str << "\n";

Here's a demo .这是一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM