For the std::priority_queue I assumed that the first template parameter specified the type and the second should be a container of that type. Example:
priority_queue<int, vector<int>> someQueue;
However, the following code compiles and seems to run fine:
class SomeClass
{
};
int main()
{
priority_queue <SomeClass, vector<int>> pq;
int x = 9;
pq.push(x);
int t = pq.top();
cout << t << endl;
pq.pop();
return 0;
}
Is the above code invalid (i.e. giving UB)?
If it is valid - what is the first template parameter (i.e. someClass
) used for in the priority_queue.
priority_queue<int>
, I suppose. It's not used anywhere else, and I don't see any requirements on it in the standard.std::priority_queue
reference you will see that the types used for the stored data actually uses the types from the container, not the provided first template argument. If it's supposed to be like that and what the specification says I don't know.priority_queue <SomeClass, vector<int>> pq;
is wrong. please refer:cplusplus.com/reference/queue/priority_queueSomeClass
? I guess it must have some use since it is there. Actually I expected a compile error for the code but it just compiled fine.