简体   繁体   English

无法重载glm :: vec2'<'运算符

[英]Failed to overload glm::vec2 '<' operator

I need to use std::map with glm::vec2 so I tried to implement '<' operator but it fails. 我需要将std :: map与glm :: vec2一起使用,因此我尝试实现'<'运算符,但失败。 ( std::map needs this operator ) (std :: map需要此运算符)

Here is the code from my test example: 这是我的测试示例中的代码:

bool operator <(const glm::vec2& l, const glm::vec2& r)
{
    int lsize = l.x + l.y;
    int rsize = r.x + r.y;

    return lsize < rsize;
}


class A
{
public:
    A()
    {
        test[glm::vec2(5, 5)] = 4;
    }
private:
    std::map<glm::vec2, int> test;
};


int _tmain(int argc, _TCHAR* argv[])
{
    A a;
    return 0;
}

When I create my own Vector2 class and implement the operator same way up there, it compiles, but it fails with glm::vec2 ( I have 19 errors telling the operator '<' is not defined for glm::vec2 etc. ) 当我创建自己的Vector2类并在那里以相同的方式实现运算符时,它会编译,但是会以glm :: vec2失败(我有19个错误,告诉您没有为glm :: vec2等定义运算符'<')

error C2676: binary '<' : 'const glm::vec2' does not define this operator or a conversion to a type acceptable to the predefined operator   

Note that my operator overloading function compiles, the error comes from the use of std::map, seems like the '<' glm::vec2 operator is still considered as undefined. 请注意,我的运算符重载函数会编译,该错误来自使用std :: map,似乎'<'glm :: vec2运算符仍被认为是未定义的。

Here is GLM sources if it can help you : https://github.com/g-truc/glm/tree/master/glm 如果可以帮助您,以下是GLM来源: https : //github.com/g-truc/glm/tree/master/glm

It concerns me that you are even trying to do this in the first place, because I think this is probably not what you actually want to do. 让我担心的是,您甚至一开始都试图这样做,因为我认为这可能不是您真正想要做的。 But if you actually want to do it, you need to put it in the right namespace. 但是,如果您确实想这样做,则需要将其放在正确的名称空间中。

namespace glm {
namespace detail {
// But don't do this!  This is not a good idea...
bool operator <(const glm::vec2& l, const glm::vec2& r)
{
    int lsize = l.x + l.y;
    int rsize = r.x + r.y;
    return lsize < rsize;
}
}
}

But wait! 可是等等! This is wrong! 这是错的!

  • vec2 aren't ordered, so implementing < doesn't make any sense mathematically vec2没有排序,因此实现<在数学上没有任何意义
  • it's not a good idea to override operators on library types 覆盖库类型的运算符不是一个好主意
  • vec2 aren't ordered, so you shouldn't be putting them in a std::map vec2没有排序,所以您不应该将它们放在std::map

A std::map keeps elements inside it ordered. 一个std::map将其中的元素保持有序。 So, what happens when you do this? 那么,当您这样做时会发生什么?

int main(int argc, char* argv[])
{
    std::map<glm::vec2, int> m;
    m[glm::vec2(1, 1)] = 10;
    std::cout << m[glm::vec2(0, 2)] << '\n';
    return 0;
}

Yes, this prints out 10 even though we never added (0, 2) to the map. 是的,即使我们从未在地图上添加(0,2),它也会打印出10

You probably want to use a spatial index (quatdree, for example) or at the very least, use a lexicographic order. 您可能要使用空间索引(例如,四进制),或者至少要使用字典顺序。 Barring that, if this is the behavior you actually want, you should be changing the comparator on your std::map via template arguments. 除非这是您真正想要的行为,否则您应该通过模板参数在std::map上更改比较器。

struct vec2_cmp {
    bool operator()(const glm::vec2 &x, const glm::vec2 &y) { ... }
}

std::map<glm::vec2, int, vec2_cmp> m;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM