gpt4 book ai didi

C++11 Android 指针 vector push_back 不适用于 libstdc++

转载 作者:太空狗 更新时间:2023-10-29 21:02:55 25 4
gpt4 key购买 nike

以下代码在使用 STLPort 时可以正常编译:

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

然而,当使用 libstdc++ 进行编译时,由于某种原因它试图使用移动构造函数:

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

这是一个非常基本的示例,但在使用 push_back 时,整个应用程序中的所有本地指针都会出现此问题。

move.h发生错误:

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

示例2(我写的另一个基本测试:)

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

我用ndk r8编译:-std=c++11 -D__STDC_INT64__

最佳答案

编译器中似乎有两个错误。首先它错误地调用了 push_back(T&&) 然后尝试移动对象,这是错误实现的:

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

它应该实现为:

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

这意味着您的编译器在此上下文中显示了两个错误:

  • 不正确的重载解析,因为它调用了 push_back(T&&)
  • std::move 的错误实现

您使用的是哪个版本的编译器?

关于C++11 Android 指针 vector push_back 不适用于 libstdc++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14373452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com