std::reference_wrapper MS Visual Studio 2013
std::reference_wrapper on MS Visual Studio 2013
我尝试编译一些与以下代码非常相似的代码:
#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;
}
但是想不通,为什么它没有编译。我很确定,相同的代码在 MS Visual Studio 2012 上编译良好 - 但在 Visual Studio 2013 上,它报告以下编译错误:
error C2280: std::reference_wrapper<const A>::reference_wrapper(_Ty &&): attempting to reference a deleted function
我尝试将复制、移动、赋值运算符添加到我的 class - 但无法消除此错误。我怎样才能准确地找出这个错误指的是哪个已删除的函数?
您想存储一个 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(本地测试)。
我尝试编译一些与以下代码非常相似的代码:
#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;
}
但是想不通,为什么它没有编译。我很确定,相同的代码在 MS Visual Studio 2012 上编译良好 - 但在 Visual Studio 2013 上,它报告以下编译错误:
error C2280: std::reference_wrapper<const A>::reference_wrapper(_Ty &&): attempting to reference a deleted function
我尝试将复制、移动、赋值运算符添加到我的 class - 但无法消除此错误。我怎样才能准确地找出这个错误指的是哪个已删除的函数?
您想存储一个 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(本地测试)。