What is the use of decltype in this code ?

I have a written a small piece of code which throws a lot of error, If I dont't use the decltype keyword while with the decltype keyword it compiles fine : -

   std::function<bool(int,int)> f2 = [dist](int n1,int n2) {if(dist[n1] < dist[n2]) return false ; return true ; } ; 
        priority_queue<int,vector<int>,decltype(f2)>  pq(f2)  ; 

Here, I wanted to declare a priority_queue with my own custom comparison function, so I decided to use std::function and lambdas. Also, dist is a std::vector<int>

But strangely , the code gives error if I replace decltype(f2) with just f2.

Why is is so ?

Answers


Referring to documentations, priority_queue must receive 3 types. Here, types int and vector<int> are followed by the type of f2 and not f2. decltype gives you a type not a variable.

Note that: decltype = typeof but in official way


Third parameter of priority_queue template class is type of predicate. Here decltype(f2) actually gives a type of f2, instead decltype you can just write std::function<bool(int,int)>.


Need Your Help

TextView doesn't get truncated in TableLayout although using ellipsize attribute

android textview tablelayout

I've got some problems with a table layout I used to show communication data within an app. The problem is that the value of the communication data may be longer than the available width so the tex...

Vertically align stubborn paragraphs

html alignment jsfiddle

I have been trying to get an 'About Us' section to vertically align to the paragraph below it. For some reason, when I try to resize the viewing area or look at it from a smaller screen, About and...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.