clang++ with c++11 flag shows rvalue reference cannot bind to lvalue error
c++, c++11, clang
Solution
Your code is fine. The problem is that you're using too old a version of libstdc++. Either update that or use libc++ instead if you have it installed (add `-stdlib=libc++` to your clang++ invocation).
Problem
I wrote a simple C++ program t.cpp: ``` // t.cpp #include <vector> #include <string> using namespace std; int main() { vector<string> strVec; string a("a"); strVec.push_back(a); return 0; } ``` Then, I compiled it by clang++ 3.4 on CentOS 6.5 x64. It can be compiled without any error by this command: ``` clang++ t.cpp ``` However, if I turned on the C++11 flag, -std=c++11: ``` clang++ -std=c++11 t.cpp ``` I got a lot of error messages: ``` In file included from t.cpp:1: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/vector:61: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:66: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:60: /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/move.h:57:14: error: rvalue reference to type 'basic_string<[3 * ...]>' cannot bind to lvalue of type 'basic_string<[3 * ...]>' { return __t; } ^~~ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:302:7: note: in instantiation of function template specialization 'std::move<std::basic_string<char> &>' requested here _GLIBCXX_MOVE(*(this->_M_impl._M_finish ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/move.h:61:33: note: expanded from macro '_GLIBCXX_MOVE' #define _GLIBCXX_MOVE(_Tp) std::move(_Tp) ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:741:4: note: in instantiation of function template specialization 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::_M_insert_aux<const std::basic_string<char> &>' requested here _M_insert_aux(end(), __x); ^ t.cpp:8:9: note: in instantiation of member function 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::push_back' requested here strVec.push_back(a); ^ In file included from t.cpp:1: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/vector:62: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/allocator.h:48: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/x86_64-redhat-linux/bits/c++allocator.h:34: /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:111:27: error: no matching function for call to 'forward' { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:301:18: note: in instantiation of function template specialization '__gnu_cxx::new_allocator<std::basic_string<char> >::construct<std::basic_string<char> >' requested here this->_M_impl.construct(this->_M_impl._M_finish, ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:741:4: note: in instantiation of function template specialization 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::_M_insert_aux<const std::basic_string<char> &>' requested here _M_insert_aux(end(), __x); ^ t.cpp:8:9: note: in instantiation of member function 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::push_back' requested here strVec.push_back(a); ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/move.h:51:5: note: candidate function [with _Tp = std::basic_string<char>] not viable: no known conversion from 'std::basic_string<char>' to 'typename std::identity<basic_string<char> >::type &&' (aka 'std::basic_string<char> &&') for 1st argument forward(typename std::identity<_Tp>::type&& __t) ^ In file included from t.cpp:1: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/vector:61: /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:561:20: error: no matching function for call to 'move' *--__result = std::move(*--__last); ^~~~~~~~~ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:593:45: note: in instantiation of function template specialization 'std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b<std::basic_string<char> *, std::basic_string<char> *>' requested here _Category>::__copy_move_b(__first, ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:602:24: note: in instantiation of function template specialization 'std::__copy_move_backward_a<true, std::basic_string<char> *, std::basic_string<char> *>' requested here return _BI2(std::__copy_move_backward_a<_IsMove> ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:674:20: note: in instantiation of function template specialization 'std::__copy_move_backward_a2<true, std::basic_string<char> *, std::basic_string<char> *>' requested here return (std::__copy_move_backward_a2<true> ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:308:4: note: in instantiation of function template specialization 'std::move_backward<std::basic_string<char> *, std::basic_string<char> *>' requested here _GLIBCXX_MOVE_BACKWARD3(__position.base(), ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:679:53: note: expanded from macro '_GLIBCXX_MOVE_BACKWARD3' #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp) ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:741:4: note: in instantiation of function template specialization 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::_M_insert_aux<const std::basic_string<char> &>' requested here _M_insert_aux(end(), __x); ^ t.cpp:8:9: note: in instantiation of member function 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::push_back' requested here strVec.push_back(a); ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/move.h:56:5: note: candidate template ignored: substitution failure [with _Tp = std::basic_string<char> &] move(_Tp&& __t) ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:491:5: note: candidate function template not viable: requires 3 arguments, but 1 was provided move(_II __first, _II __last, _OI __result) ^ In file included from t.cpp:1: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/vector:61: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:69: /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_iterator.h:916:16: error: rvalue reference to type 'basic_string<[3 * ...]>' cannot bind to lvalue of type 'basic_string<[3 * ...]>' { return *_M_current; } ^~~~~~~~~~~ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_uninitialized.h:74:28: note: in instantiation of member function 'std::move_iterator<std::basic_string<char> *>::operator*' requested here std::_Construct(&*__cur, *__first); ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_uninitialized.h:116:2: note: in instantiation of function template specialization 'std::__uninitialized_copy<false>::uninitialized_copy<std::move_iterator<std::basic_string<char> *>, std::basic_string<char> *>' requested here uninitialized_copy(__first, __last, __result); ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_uninitialized.h:256:19: note: in instantiation of function template specialization 'std::uninitialized_copy<std::move_iterator<std::basic_string<char> *>, std::basic_string<char> *>' requested here { return std::uninitialized_copy(__first, __last, __result); } ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_uninitialized.h:264:19: note: in instantiation of function template specialization 'std::__uninitialized_copy_a<std::move_iterator<std::basic_string<char> *>, std::basic_string<char> *, std::basic_string<char> >' requested here return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first), ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:339:8: note: in instantiation of function template specialization 'std::__uninitialized_move_a<std::basic_string<char> *, std::basic_string<char> *, std::allocator<std::basic_string<char> > >' requested here std::__uninitialized_move_a(this->_M_impl._M_start, ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:741:4: note: in instantiation of function template specialization 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::_M_insert_aux<const std::basic_string<char> &>' requested here _M_insert_aux(end(), __x); ^ t.cpp:8:9: note: in instantiation of member function 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::push_back' requested here strVec.push_back(a); ^ In file included from t.cpp:1: In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/vector:63: /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_construct.h:73:42: error: no matching function for call to 'forward' ::new(static_cast<void*>(__p)) _T1(std::forward<_T2>(__value)); ^~~~~~~~~~~~~~~~~ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_uninitialized.h:74:8: note: in instantiation of function template specialization 'std::_Construct<std::basic_string<char>, std::basic_string<char> >' requested here std::_Construct(&*__cur, *__first); ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_uninitialized.h:116:2: note: in instantiation of function template specialization 'std::__uninitialized_copy<false>::uninitialized_copy<std::move_iterator<std::basic_string<char> *>, std::basic_string<char> *>' requested here uninitialized_copy(__first, __last, __result); ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_uninitialized.h:256:19: note: in instantiation of function template specialization 'std::uninitialized_copy<std::move_iterator<std::basic_string<char> *>, std::basic_string<char> *>' requested here { return std::uninitialized_copy(__first, __last, __result); } ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_uninitialized.h:264:19: note: in instantiation of function template specialization 'std::__uninitialized_copy_a<std::move_iterator<std::basic_string<char> *>, std::basic_string<char> *, std::basic_string<char> >' requested here return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first), ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:339:8: note: in instantiation of function template specialization 'std::__uninitialized_move_a<std::basic_string<char> *, std::basic_string<char> *, std::allocator<std::basic_string<char> > >' requested here std::__uninitialized_move_a(this->_M_impl._M_start, ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:741:4: note: in instantiation of function template specialization 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::_M_insert_aux<const std::basic_string<char> &>' requested here _M_insert_aux(end(), __x); ^ t.cpp:8:9: note: in instantiation of member function 'std::vector<std::basic_string<char>, std::allocator<std::basic_string<char> > >::push_back' requested here strVec.push_back(a); ^ /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/move.h:51:5: note: candidate function [with _Tp = std::basic_string<char>] not viable: no known conversion from 'std::basic_string<char>' to 'typename std::identity<basic_string<char> >::type &&' (aka 'std::basic_string<char> &&') for 1st argument forward(typename std::identity<_Tp>::type&& __t) ^ 5 errors generated. ``` One error is: ``` error: rvalue reference to type 'basic_string<[3 * ...]>' cannot bind to lvalue of type 'basic_string<[3 * ...]>' ``` What does it mean?