【发布时间】:2020-05-15 15:18:36
【问题描述】:
我想要一个没有复制 ctor、没有移动 ctor 和默认 ctor 的类的映射(我无法控制这个类)。
我尝试过将 std::map::emplace 与 std::piecewise_construct 和 std::forward_as_tuple 参数一起使用,但编译器似乎告诉我这是不可能的,因为它首先尝试默认构造。
// 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";
}
在此处查看编译器错误示例:http://cpp.sh/8bbwh
我怎样才能做到这一点?我试过查看类似的问题,但它们似乎没有包含任何对我的特定场景有用的答案。
【问题讨论】: