PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

[FIXED] Why don't we add parenthesis when writing comparator in c++?

 November 04, 2022     algorithm, c++, comparator, lambda, sorting     No comments   

Issue

Here is a code explaining what I mean.


static bool comparator(int a, int b) {
    if(a > b) return false;
    return true;
}

sort(arr.begin(), arr.end(), comparator); // why don't we write comparator()


Solution

If you will write

sort(arr.begin(), arr.end(), comparator());

then it means that the argument expression of the function std::sort comparator() must be evaluated. But neither arguments are supplied to the function call comparator(). So the compiler will issue an error message.

On the other hand, if you will write

sort(arr.begin(), arr.end(), comparator);

then the function designator comparator is implicitly converted to a pointer to the function and the function std::sort within itself will call the function using the pointer and supplying two arguments to it.



Answered By - Vlad from Moscow
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing