int compare operator as a function

Jul 15, 2019 at 4:10pm
Hi everyone. I want to get the int compare operator <, =, > as functions.
If I can get them as function, I can treat them as other function's parameter for call back.
Is that possible?

Edit:
I used to do such thing a lot at functional programming language.
But have operator as parameter question in C++.
If I want to implement these things, they are super easy:
1
2
3
4
5
inline bool equal(uint64_t first, uint64_t second) { return first == second; }

inline bool greater_than(uint64_t first, uint64_t second) { return first > second; }

inline bool less_than(uint64_t first, uint64_t second) { return first < second; }

Last edited on Jul 15, 2019 at 4:18pm
Jul 15, 2019 at 4:16pm
probably not.
this is built into all cpu, and so the compiler is using the JNE or something CPU instruction.

you will have to write your own.

think about your design again. If possible, avoid this idea: you are probably looking to introduce java-like slowness into your code here, and it may be avoidable.
Last edited on Jul 15, 2019 at 4:18pm
Jul 15, 2019 at 4:20pm
Thanks jonnin.
The reason why I am asking, is also concerned with your answer.
I thought these functions implemented by me is not so fast as the normal >, = , < .
Do you see some places to improve the performance?
Jul 15, 2019 at 4:27pm
they will not be as fast if you make your own.
if you are doing a LOT (billion+) of them, it will begin to be an issue. Is it going to be an issue with your project?

If so, the answer is 'can you do it without a call-back at all' ?
if not, you may be stuck.
if you can, just avoid the call-back overhead and let the CPU do the CPU's work.

What exactly do you want to accomplish?
Last edited on Jul 15, 2019 at 4:30pm
Jul 15, 2019 at 4:28pm
CakeByTheOcean wrote:
I want to get the int compare operator <, =, > as functions.

http://www.cplusplus.com/reference/functional/
See:
less, equal_to, greater
Topic archived. No new replies allowed.