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;
}

I am trying to use std::reference_wrapper to hold references to type "foo" but when I do this I get a very peculiar compiler error:

>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> >]

It is impossible to discern what is going on. I have tried the same code on VS 2010 and it worked. I am wondering why it's not working for g++

The command I entered in terminal was:

 g++ -std=gnu++0x reference_wrapper_test.cpp -o reference_wrapper_test
7
  • 1
    no matching function for call to \u2018std::list, std::allocator > >::push_back(foo&). That explains why MSVC accepts it. It has that evil extension. Commented Feb 16, 2013 at 4:07
  • variable names are weird. Commented Feb 16, 2013 at 4:09
  • Alright so how do go about accomplishing the above functionality with g++? Commented Feb 16, 2013 at 4:11
  • 1
    @XYZ Update to g++ 4.7: liveworkspace.org/code/1tmztV$0 (4.6 seems to work,too.) Commented Feb 16, 2013 at 4:48
  • 2
    gcc 4.4.6 is just ages old. Commented Feb 16, 2013 at 7:32

1 Answer 1

2

I think that you could update your compiler .

See the Link : http://liveworkspace.org/code/3mVcGp$6

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.