0

As title. Example:

#include <queue>
#include <vector>

class Foo {
    int n;
};

class Greater {
    const Foo& foo;

public:
    explicit Greater(const Foo& _foo): foo(_foo) {}

    bool operator()(int a, int b) const {
        return a > b;
    }
};

int main(void) {
    Foo foo;
    std::priority_queue<int, std::vector<int>, Greater> my_queue(Greater(foo));
    my_queue.push(30);

    return 0;
}

And I got the following error:

file.cpp: In function 'int main()':
file.cpp:22:14: error: request for member 'push' in 'my_queue', which is of 
non-class type 'std::priority_queue<int, std::vector<int>, Greater>(Greater)'
    my_queue.push(30);
             ^

Looks like compiler thought I'm declaring a function named 'my_queue' that receives Greater and returns a std::priority_queue.

I know I can do:

Greater greater(foo);
std::priority_queue<int, std::vector<int>, Greater> my_queue(greater);

and that compiled fine.

My question is, is that a bug in compiler/parser? What's prevented it from recognizing I'm trying to call Greater's constructor instead of declaring parameter Greater?

I'm using GCC version 4.9.3.

Thank you.

dianhenglau
  • 484
  • 5
  • 9

0 Answers0