Creating a priority_queue
with two integers like that:
std::priority_queue<int> pq(a, b);
Was till recently treated by most compilers as legal, sending the parameters to the underlying container, thus creating a queue holding a
times b
. Although the constructor of priority_queue
that is used for that is actually expecting two iterators:
std::priority_queue(InputIterator begin, InputIterator end);
Which is constructor #7 or #8 depending on the C++ version, as listed in https://en.cppreference.com/w/cpp/container/priority_queue/priority_queue
The arguments provided do not meet the InputIterator
requirements.
And as it seems, most compilers fixed this issue recently and produce compilation error, see:
https://godbolt.org/z/Tqxdzc75d
It is interesting to note that the spec [priority.queue] uses the term InputIterator
, but without clearly requiring the arguments to obey to InputIterator
requirements. Well as cppreference does require that for constructors 7 to 9:
Iterator-pair constructors. These overloads participate in overload resolution only if InputIt satisfies LegacyInputIterator.
It is quite clear that these constructors are meant for iterators, for example for use case like that:
std::vector<int> v(14, 12);
std::priority_queue<int> p(v.begin(), v.end()); // 14 times 12, now legit
Or, if you want two values - 14 and 12:
std::vector<int> v{14, 12};
std::priority_queue<int> p(v.begin(), v.end()); // 14, 12 - also legit
Bottom line: the original code shall fail compilation.
EDIT
It seems that the recent change is based on LWG defect #3522: Missing requirement on InputIterator template parameter for priority_queue constructors - this made the OP's code illegal and added the requirement for the InputIterator
parameters to be actually iterators, already reflected in cppreference, as mentioned above.