Grapes
Grapes

Reputation: 2583

C++11 Android vector of pointers push_back doesn't work with libstdc++

The following code compiles fine when using STLPort:

std::vector<Engine::Screen::IOverlay*> Overlays;
auto TestOverlay=new Engine::Screen::Overlay();
Overlays.push_back(TestOverlay);

However when compiling with libstdc++ it's trying to use move constructor for some reason:

error : cannot bind 'Engine::Screen::IOverlay*' lvalue to 'Engine::Screen::IOverlay*&&' ...\android-ndk-r8\sources\cxx-stl\gnu-libstdc++\include\bits\move.h

This is a very basic example but this issue occurs through out the application for all local pointers when using push_back.

Error occurs in move.h:

template<typename _Tp>
inline typename std::remove_reference<_Tp>::type&&
move(_Tp&& __t)
{ return __t; }

Example 2 (Another basic test I wrote:)

class TestClass {};
auto TestInstance=new TestClass;
std::vector<TestClass*> TestVector;
TestVector.push_back(TestInstance);

I compiled with ndk r8: -std=c++11 -D__STDC_INT64__

Upvotes: 3

Views: 830

Answers (1)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361612

It seems there are two bugs in the compiler. First it incorrectly callspush_back(T&&) which then attempts to move the object, which is implemented incorrecty:

template<typename _Tp>
inline typename std::remove_reference<_Tp>::type&&
move(_Tp&& __t)
{ return __t; }

It should be implemented as:

template<class _Tp> 
typename remove_reference<_Tp>::type&&
move(_Tp&& __t) noexcept //noexcept should be here!
{
  return static_cast<typename remove_reference<_Tp>::type&&>(__t);
}

which means your compiler is showing up two bugs in this context:

  • incorrect overload resolution, as it calls push_back(T&&).
  • incorrect implementation of std::move

which version of what compiler are you using?

Upvotes: 4

Related Questions