std::remove_if 将“const type”作为“this”参数传递会丢弃 Linux 上的限定符
std::remove_if passing ‘const type’ as ‘this’ argument discards qualifiers on linux
由于我昨天的帖子被投票了,这里再次,只有最少的例子和我的问题。
#include <set>
#include <algorithm>
using namespace std;
class dummy
{
public:
dummy(int x)
: test(x)
{}
bool operator()(const int &a) const
{
return false;
}
protected:
int test;
};
void foo()
{
// Determine the bounding box.
multiset<float> test;
test.insert(3.5);
multiset<float>::iterator itVertex = test.begin();
multiset<int> workset;
workset.insert(3);
for (itVertex = test.begin(); itVertex != test.end(); itVertex++)
{
multiset<int>::iterator itEnd = remove_if(workset.begin(), workset.end(), dummy(3));
}
}
我已经在两台机器上测试了该示例:在 Linux(Ubuntu 16.04、g++ 5 和 6)上,我收到前面描述的错误:
/usr/include/c++/5/bits/stl_algo.h:868:23:错误:分配只读位置"__result.std::_Rb_tree_const_iterator<_Tp>::operator*()" __result = _GLIBCXX_MOVE(__first);
在Windows 7 Visual Studio 2008上,它可以很好地编译。
我也检查了重复项,但它们无法描述它在Windows上工作但在Linux上不起作用的情况。
那么为什么它在Windows上编译得很好呢?为什么不在 Linux 上呢?我了解如何解决问题本身,但由于这是外部代码,如果没有必要,我不想编辑代码本身。
std::remove_if
不适用于多集。它仅适用于可以重新排序的容器,因为要删除的元素会暂时被其他可以填补空白的元素覆盖。
使用 multiset
,您不能覆盖给定迭代器的值,因为迭代器是定义的排序函数和当前内容的乘积。
如果这适用于 MSVC,那么 STL 实现的作用超出了标准要求。依靠它不安全,因为它不能保证。
相关文章:
- 在C++/Linux中设置单调时钟的一些技巧
- Linux的Cpp上的计时器
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 在Linux中哪里可以找到互斥、未来等的源代码
- 在 Linux 上C++:在作为 systemd 服务运行时侦听键盘输入
- 在 C++ 年的 Linux 中实现对非基于文本的文件作为 wc 的字数统计
- std::remove_if 将“const type”作为“this”参数传递会丢弃 Linux 上的限定符
- 使用字符串作为模板类型时,C++ Linux 中的核心转储
- 作为共享库的 Linux 可执行文件
- Soci 作为 linux/ubuntu 上的静态库
- C++ Linux 套接字,它在单个调用中将字符串数组 (char**) 作为串联字符串发送
- Linux 将字符串值作为十六进制写入串行
- Linux将连续输出作为命令行参数传递
- Linux套接字作为多播服务器和客户端
- 是否有可能通过远程方法调用将linux中的c++程序与作为服务器的c#程序进行通信?
- 在Linux下使用连字符或下划线作为文件的分隔符
- Linux上预编译的头警告(太短而不能作为PCH文件)
- Linux:C++抽象类作为共享对象API
- 使用多个空格 (>1) 作为分隔符,使用 C++ 或 linux 将一行分隔为列