我有一段代码在运行时因各种奇怪的内存损坏问题而失败。我已将范围缩小到这部分代码:
List<CollisionBlock> WorldClient::getCollisionBlocks(RectF const& boundBox, bool doSort, Vec2F sortCenter) const {
auto res = m_collisionGenerator.getPolys(boundBox);
if (doSort) {
sort(res, [=](CollisionBlock const& block1, CollisionBlock const& block2) {
return magSquared(sortCenter - block1.poly.center()) < magSquared(sortCenter - block2.poly.center());
});
}
return res;
}
如果我从 lambda 中删除 const&
,代码就可以正常工作。我不知道为什么。我想知道我是否遇到了编译器错误,或者是否有明显的东西被我忽略了。
这是 CollisionBlock 的定义:
struct CollisionBlock {
PolyF poly;
// Will never be None
CollisionKind kind;
// Normalzied vector encoding the slope of the block we collided with.
// Always faces right, y component can be positive or negative.
Vec2F slope;
};
我可以在 Linux 32 位(g++ 版本 4.7.0 和 4.6.3)、MacOSX(不确定字长和 g++ 版本)、Windows 7 64 位(g++ 版本 4.6.3)、Windows 7 32-位(g++ 版本 4.6.2 和 4.6.3),但不是 Linux 64 位(g++ 版本 4.6.1)。
我使用的是 C++11,而不是 boost。
Poly::center()
Coord center() const {
return sum(m_vertexes) / (DataType)m_vertexes.size();
}
总和
template<typename Container>
typename Container::value_type sum(Container const& cont) {
return reduce(cont, std::plus<typename Container::value_type>());
}
减少
// Somewhat nicer form of std::accumulate
template<typename Container, typename Function>
typename Container::value_type reduce(Container const& l, Function f) {
typename Container::const_iterator i = l.begin();
typename Container::value_type res{};
if (i == l.end())
return res;
res = *i++;
while (i != l.end())
res = f(res, *i++);
return res;
}
排序
template<typename Container, typename Compare>
void sort(Container& c, Compare comp) {
std::sort(c.begin(), c.end(), comp);
}
这个问题有很多问题。抱歉,我会尝试制定一个更小的测试用例。
更新:
用 std::accumulate
替换 Poly::center
中对 sum
的调用没有帮助。
最佳答案
我相当确定错误出在您发布的代码之外。你可以把我下面的虚拟代码慢慢地变成你的真实代码,直到它开始出现问题。如果您找到它,请告诉我们,我很好奇。
仅供引用,我不得不在几个地方编写代码以获得接近你正在做的事情。这一切的真正作用是练习您发布的一小段代码。
#include <list>
#include <deque>
#include <vector>
#include <algorithm>
typedef double mocish;
typedef int CollisionKind; //is actually enum class
typedef mocish RectF;
class Vec2F {
public:
Vec2F() {
vertexes.push_back(0);
vertexes.push_back(0);
}
Vec2F(float a, float b) {
vertexes.push_back(a);
vertexes.push_back(b);
}
float operator[](unsigned index) const {
return vertexes[index];
}
float operator[](unsigned index) {
return vertexes[index];
}
Vec2F operator+(Vec2F const& other) const {
return Vec2F(vertexes[0]+other[0], vertexes[1]+other[1]);
}
Vec2F operator-(Vec2F const& other) const {
return Vec2F(vertexes[0]-other[0], vertexes[1]-other[1]);
}
Vec2F operator*(float other) const {
return Vec2F(vertexes[0]*other, vertexes[1]*other);
}
Vec2F operator/(float other) const {
return Vec2F(vertexes[0]/other, vertexes[1]/other);
}
Vec2F operator=(Vec2F const& other) {
vertexes[0] = other[0];
vertexes[1] = other[1];
return *this;
}
private:
std::deque<float> vertexes;
};
float magSquared(Vec2F const& a) {
return a[0]*a[0]+a[1]*a[1];
}
typedef Vec2F Coord;
// Somewhat nicer form of std::accumulate
template<typename Container, typename Function>
typename Container::value_type reduce(Container const& l, Function f) {
typename Container::const_iterator i = l.begin();
typename Container::value_type res{};
if (i == l.end())
return res;
res = *i++;
while (i != l.end())
res = f(res, *i++);
return res;
}
template<typename Container>
typename Container::value_type sum(Container const& cont) {
return reduce(cont, std::plus<typename Container::value_type>());
}
struct PolyF
{
PolyF()
{
m_vertexes.resize(4);
std::generate( m_vertexes.begin(), m_vertexes.end(), [](){ return Vec2F(std::rand(), std::rand());} );
}
std::vector<Coord> m_vertexes;
Coord center() const
{
return sum(m_vertexes) / (float)m_vertexes.size();
}
};
struct CollisionBlock
{
PolyF poly;
// Will never be None
CollisionKind kind;
// Normalzied vector encoding the slope of the block we collided with.
// Always faces right, y component can be positive or negative.
Vec2F slope;
};
template<typename Container, typename Compare>
void sort(Container& c, Compare comp) {
std::sort(c.begin(), c.end(), comp);
}
struct CollisionGen
{
std::deque<CollisionBlock> getPolys( RectF const& ) const
{
std::deque<CollisionBlock> collision_block_moc(50);
return collision_block_moc;
}
};
struct WorldClient
{
CollisionGen m_collisionGenerator;
std::deque<CollisionBlock> getCollisionBlocks(RectF const& boundBox, bool doSort, Vec2F sortCenter) const
{
auto res = m_collisionGenerator.getPolys(boundBox);
//auto test = magSquared(sortCenter - res.front().poly.center()) < magSquared(sortCenter - res.front().poly.center());
if (doSort) {
sort(res, [=](CollisionBlock const& block1, CollisionBlock const& block2) {
return magSquared(sortCenter - block1.poly.center()) < magSquared(sortCenter - block2.poly.center());
});
}
return res;
}
};
int main()
{
WorldClient wc;
while (true) {
wc.getCollisionBlocks( 42.0, true, {0,0} );
}
}
关于c++ - 奇怪的 lambda 问题(编译器错误?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11254305/