2013-02-16 3 views
1
#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; 
} 

나는 "foo는"입력에 대한 참조를 개최 표준 : reference_wrapper을 사용하려고하지만이 작업을 수행 할 때 매우 독특한 컴파일러 오류 얻을 :컴파일러 오류 :: reference_wrapper

>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

'일치 기능 :

은 링크를 참조하십시오. 이것이 MSVC가 왜 그것을 받아들이는지 설명해줍니다. 그 악한 확장이 있습니다. – chris

+0

변수 이름이 이상합니다. – Nawaz

+0

그래, 어떻게 g ++로 위의 기능을 수행 할 수 있을까요? – TTV

답변

2

나는 당신이 당신의 컴파일러를 업데이트 할 수 있다고 생각합니다. 호출이 :: 목록, 표준 : : 할당>> ::와 push_back (foo는 &)를`u2018std \하는 http://liveworkspace.org/code/3mVcGp $ (6)