gpt4 book ai didi

c++ - std::reference_wrapper 的编译器错误

转载 作者:行者123 更新时间:2023-11-30 04:20:58 26 4
gpt4 key购买 nike

#include <functional>
#include <algorithm>
#include <list>
using namespace std;

struct foo{
int _val;
};

int main(){
list<foo> A;
foo B;
for(int i=0;i<10;++i){
B._val=i;
A.push_back(B);
}
list< reference_wrapper < foo > > C(A.begin(),A.end());
return 0;
}

我正在尝试使用 std::reference_wrapper 来保存对类型“foo”的引用,但是当我这样做时,我遇到了一个非常奇怪的编译器错误:

>In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/list:63,
from ref_wrapper.cpp:4:/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_list.h: In member function \u2018void std::list<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = std::_List_iterator<foo>, _Tp = std::reference_wrapper<foo>, _Alloc = std::allocator<std::reference_wrapper<foo> >]\u2019:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_list.h:577: instantiated from \u2018std::list<_Tp, _Alloc>::list(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = std::_List_iterator<foo>, _Tp = std::reference_wrapper<foo>, _Alloc = std::allocator<std::reference_wrapper<foo> >]\u2019
ref_wrapper.cpp:19: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_list.h:1361: error: no matching function for call to \u2018std::list<std::reference_wrapper<foo>, std::allocator<std::reference_wrapper<foo> > >::push_back(foo&)\u2019
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_list.h:919: note: candidates are: void std::list<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::reference_wrapper<foo>, _Alloc = std::allocator<std::reference_wrapper<foo> >]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_list.h:924: note: void std::list<_Tp, _Alloc>::push_back(_Tp&&) [with _Tp = std::reference_wrapper<foo>, _Alloc = std::allocator<std::reference_wrapper<foo> >]

无法辨别发生了什么。我在 VS 2010 上尝试了相同的代码并且它有效。我想知道为什么它不适用于 g++

我在终端输入的命令是:

 g++ -std=gnu++0x reference_wrapper_test.cpp -o reference_wrapper_test

最佳答案

我认为你可以更新你的编译器。

查看链接:http://liveworkspace.org/code/3mVcGp $6

关于c++ - std::reference_wrapper 的编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14906802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com