Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Vector2i overloading operator <  (Read 5179 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
sf::Vector2i overloading operator <
« 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:
Code: [Select]
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:
Code: [Select]
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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Re: sf::Vector2i overloading operator <
« Reply #1 on: August 11, 2008, 07:31:03 pm »
Quote from: "Nexus"
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Vector2i overloading operator <
« Reply #2 on: August 12, 2008, 12:57:56 pm »
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.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
sf::Vector2i overloading operator <
« Reply #3 on: August 12, 2008, 01:10:48 pm »
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).

Quote from: "Laurent"
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:
Quote
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>        ]


Quote from: "Laurent"
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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Vector2i overloading operator <
« Reply #4 on: August 12, 2008, 03:25:32 pm »
Quote
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.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
sf::Vector2i overloading operator <
« Reply #5 on: August 13, 2008, 01:46:45 am »
"Meaningless" because normally, you don't compare mathematical vectors? So "meaningful" operations are more common (or defined), or what do you exactely mean?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
sf::Vector2i overloading operator <
« Reply #6 on: August 13, 2008, 04:25:41 am »
Quote from: "Nexus"
"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>)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
sf::Vector2i overloading operator <
« Reply #7 on: August 13, 2008, 05:39:35 pm »
Okay, thanks. Now I wrote an own functor, like this I don't have to intrude into namespace sf, either ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything