1
votes

The following code fragment declares a priority_queue of type pair<int,int> and uses a class comparator and correct in C++11 but shows error in C++03. What is the reason?

class compare
    {
    public:

        bool operator () (pair<int,int>&p1,pair<int,int>&p2)
        {
            return p1.second > p2.second;
        }
    };
    priority_queue <pair<int,int>, vector<pair<int,int> >,compare>pq;    

C++03 compiler shows:

  • error: template argument for 'template class std::priority_queue' uses local type 'main()::compare' priority_queue , vector >,compare>pq;
  • error: trying to instantiate 'template class std::priority_queue'
1
What is C++ 5.1? (hint: nothing; it doesn't exist) - Lightness Races in Orbit
@BarryTheHatchet I'd suspect G++ 5.1, though c++11 standard should be the default for that version. - πάντα ῥεῖ
@πάνταῥεῖ: I suspect Borland Turbo C++ 5.01 for DOS emulators, but we'd have to allow for the dropping of that 0 for that to be true. - Lightness Races in Orbit
@BarryTheHatchet Good point of confusion ;-) ... - πάντα ῥεῖ
@BarryTheHatchet I doubt it, it's from before C++ standardization and probably won't have a class like priority_queue. - Some programmer dude

1 Answers

1
votes

Before C++11, it was impossible to pass local types as template arguments.

That's why your C++03 compiler rejects the code.

If you're using a modern compiler, you can flip it into C++11 or C++14 mode.
Otherwise, you're out of luck.


[C++11: C.2.6]: Clause 14: templates

[..]

14.6.4.2
Change: Allow dependent calls of functions with internal linkage
Rationale: Overly constrained, simplify overload resolution rules.
Effect on original feature: A valid C++ 2003 program could get a different result than this International Standard.