我尝试编译一些与:
非常相似的代码#include <string>
#include <unordered_map>
class A{
};
int main(int argc, char* argv[]){
std::unordered_map<std::string, std::reference_wrapper<const A>> stringToRef;
A a;
const A& b = a;
stringToRef.insert(std::make_pair("Test", b));
return 0;
}
但不知道,为什么它不编译。我敢肯定,在2012年MS Visual Studio上编译了相同的代码 - 但是在Visual Studio 2013上,它报告了以下汇编错误:
error C2280: std::reference_wrapper<const A>::reference_wrapper(_Ty &&): attempting to reference a deleted function
我尝试将复制,移动,分配运算符添加到我的课程中 - 但无法摆脱此错误。我该如何确切地找出删除函数此错误?
您要存储一个std::reference_wrapper<const A>
,因此您可以使用[std::cref][1]
直接从a
:
#include <functional>
#include <string>
#include <unordered_map>
#include <utility>
class A{
};
int main(int argc, char* argv []){
std::unordered_map<std::string, std::reference_wrapper<const A>> stringToRef;
A a;
stringToRef.insert(std::make_pair("Test", std::cref(a)));
return 0;
}
这与GCC/Clang libstdc ,clang libc 和MSVS 2013一起使用(本地测试)。