Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between std::greater<int>() and std::greater<int>?

This code works:

#include <iostream>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
int main(){
    priority_queue<int,vector<int>,greater<int> > pq;
    pq.push(1);
    cout<<pq.top()<<endl;
}

But,this code does not compile:

#include <iostream>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
int main(){
    priority_queue<int,vector<int>,greater<int>() > pq;
    pq.push(1);
    cout<<pq.top()<<endl;
}

Why?
All I understand is that greater<int>() is a function object and priority_queue accepts a binary predicate as third argument and that predicates are a special type of functors. But how does the pair of braces make that difference.

like image 423
Manohar Avatar asked May 29 '17 11:05

Manohar


People also ask

What does greater int >() do in C++?

Using greater<int>() in sort() Similar to the less<int>() function, the greater<int>() function returns a bool value as true or false but in the opposite sense. If the first argument is greater than the second one, the function returns true and false if the above condition is false.

What is std:: greater<>?

std::greater template <class T> struct greater; Function object class for greater-than inequality comparison. Binary function object class whose call returns whether the its first argument compares greater than the second (as returned by operator > ).

What is greater int Cpp?

std::greater in C++ with Examples The std::greater is a functional object which is used for performing comparisons. It is defined as a Function object class for the greater-than inequality comparison.

What is greater function?

Greater<T> is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class greater<T> and x and y are objects of class T, then f(x,y) returns true if x > y and false otherwise.


1 Answers

In this declaration

priority_queue<int,vector<int>,greater<int> > pq;

the type template argument greater<int> corresponds to the type of a structure.

In this declaration

priority_queue<int,vector<int>,greater<int>() > pq;

the type template argument greater<int>() corresponds to the type of a function that has no parameters and has the return type greater<int>

The class template std::priority_queue expects that the argument will be of a function object type that is a pointer to function or a class type that has a function operator.

To make it more clear compare for example these declarations

std::vector<int()> v1;

and

std::vector<int (*)()> v2;

For the first declaration the compiler will issue an error because the operator sizeof may not be applied to a function type int() and the vector will be unable to allocate memory for its elements. Here int() used as a type template argument is not an expression. It is a type-id.

In the second declaration the vector deal with pointers to function and it can allocate memory for its elements that are pointers.

like image 142
Vlad from Moscow Avatar answered Nov 05 '22 09:11

Vlad from Moscow