无法编译代码,因为它已在 C++11 中弃用

Can not compile the code because it is deprecated in C++11

本文关键字:C++11 编译 代码 因为      更新时间:2023-10-16

我有一个使用 ns-3 编译的代码,但是当我尝试编译它们时遇到了一些问题。我认为原因可能是某些函数在 C++11 中被弃用。但是我不知道如何修改代码。

这是我认为应该修改的代码:

m_sendBuffer.erase(
std::remove_if(
m_sendBuffer.begin(), m_sendBuffer.end(),
std::bind2nd(std::ptr_fun(DsrSendBuffer::IsEqual), dst)
),
m_sendBuffer.end()
);

这是错误:

../src/dsr/model/dsr-rsendbuff.cc:102:55: error: 'ptr_fun<ns3::dsr::DsrSendBuffEntry, ns3::Ipv4Address, bool>' is deprecated [-Werror,-Wdeprecated-declarations]
std::bind (std::ptr_fun (DsrSendBuffer::IsEqual), dst)), m_sendBuffer.end ());
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:1115:1: note: 'ptr_fun<ns3::dsr::DsrSendBuffEntry, ns3::Ipv4Address, bool>' has been explicitly marked deprecated here
_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:1101:39: note: expanded from macro '_LIBCPP_DEPRECATED_IN_CXX11'
define _LIBCPP_DEPRECATED_IN_CXX11 _LIBCPP_DEPRECATED
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:1090:48: note: expanded from macro '_LIBCPP_DEPRECATED'
define _LIBCPP_DEPRECATED __attribute__ ((deprecated))

似乎他们确实使用了已弃用的 API,但您仍然可以通过允许将弃用警告视为警告来编译它。将此添加到编译器选项中:

-Wno-error=deprecated-declarations

如果您想升级代码,可以改为执行以下 lambda:

m_sendBuffer.erase(
std::remove_if(
m_sendBuffer.begin(), m_sendBuffer.end(),
[&dst](auto const& buf) { return DsrSendBuffer::IsEqual(buf, dst); }
),
m_sendBuffer.end()
);

您可以从ns-3 @ gitlab克隆新版本的ns-3

std::ptr_funstd::bind2nd的使用已于 2019 年 8 月 25 日删除。