使用std :: reference_wrapper编译器错误

时间:2013-02-16 04:04:29

标签: c++ c++11

#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

1 个答案:

答案 0 :(得分:2)

我认为您可以更新编译器。

请参阅链接:http://liveworkspace.org/code/3mVcGp $ 6