SFML community forums
Help => General => Topic started by: Nexus on August 11, 2008, 02:04:04 pm
-
Hi,
I need to overload operator < for the sf::Vector2i struct because I use a std::map with it as a key value, and for the intern sort criterion I use std::less which requires operator <. Now I tried the following:
bool operator <(sf::Vector2i Left, sf::Vector2i Right)
{
if (Left.x != Right.x)
return (Left.x < Right.x);
else
return (Left.y < Right.y);
}
There are many errors. So I placed it into namespace sf:
namespace sf
{
bool operator <(sf::Vector2i Left, sf::Vector2i Right)
{
if (Left.x != Right.x)
return (Left.x < Right.x);
else
return (Left.y < Right.y);
}
}
which works.
Now I have doubts if this is the good way to solve it. Doesn't it matter if I expand the sf-namespace myself?
-
Now I have doubts if this is the good way to solve it. Doesn't it matter if I expand the sf-namespace myself?
Well, that's likely to happen if you start modifying SFML classes. If you just derive off of SFML, you won't have to touch the namespace, but you can't do everything that way.
Edit: you should be clearly commenting changes you make and why for merging purposes with future SFML. In general, adding functions is safer than modding existing ones.
-
It should work without putting the operator in sf namespace. What exactly are the "many errors" ?
Plus, you don't really need to define a < operator, you can instead provide a functor as an extra parameter of your std::map. In this case I think it would be more appropriate, as your definition of operator < for vectors is not obvious (actually, vectors are not comparable in pure maths). It's just something that works.
-
But actually, I do not want to write own vector classes which derive from sf::Vector2i... I mean the sf::Vector is okay, I just need to overload operator <.
And about modding SFML: My intention is not to change the code in the SFML source or header files; if I needed something in namespace sf, I would write it in my own project (inside a local namespace sf block).
It should work without putting the operator in sf namespace. What exactly are the "many errors" ?
I get errors because std::map is trying to use std::operator < instead of my version:
1>c:\program files\microsoft visual studio 9.0\vc\include\functional(143) : error C2784: "bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)": template-Argument für "const std::basic_string<_Elem,_Traits,_Alloc> &" konnte nicht von "const sf::Vector2i" hergeleitet werden.
1> c:\program files\microsoft visual studio 9.0\vc\include\string(150): Siehe Deklaration von 'std::operator <'
1> c:\program files\microsoft visual studio 9.0\vc\include\functional(142): Bei der Kompilierung der Klassen-template der bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const-Memberfunktion
1> with
1> [
1> _Ty=sf::Vector2i
1> ]
Plus, you don't really need to define a < operator, you can instead provide a functor as an extra parameter of your std::map. In this case I think it would be more appropriate, as your definition of operator < for vectors is not obvious (actually, vectors are not comparable in pure maths). It's just something that works.
I thought about creating an own functor, too, but if there is already the std::less criterion which has the same function, why shouldn't I use it and create an overloaded operator?
But yes, you're right, comparing too vectors doesn't make too much sense. In don't really need to compare them except for the automatic sort inside std::map.
-
I thought about creating an own functor, too, but if there is already the std::less criterion which has the same function, why shouldn't I use it and create an overloaded operator?
std::less is a default functor for classes having a meaningful operator <
For all others, using your own functor is better than adding a meaningless operator < to the class.
-
"Meaningless" because normally, you don't compare mathematical vectors? So "meaningful" operations are more common (or defined), or what do you exactely mean?
-
"Meaningless" because normally, you don't compare mathematical vectors? So "meaningful" operations are more common (or defined), or what do you exactely mean?
By convention, you don't overload an operator unless it is very clear what it does. There is no universally-accepted mathematical determination of whether <1,5> is greater than or less than <2,4>. Do you sum the members and then compare? Do you just compare the first or second member? No one knows.
So it might be a better idea to write a function with a clear name (i.e. bool sf::vector2<type>::IsVectorSumGreater(const sf::vector2<type>)
-
Okay, thanks. Now I wrote an own functor, like this I don't have to intrude into namespace sf, either ;)