Search code examples
c++templatesgccmost-vexing-parse

Meaning of class(*)() in gcc


I'm having trouble understanding this compiler error. I wrote class(*)() in the post title because the class I am instantiating is called "opaque", but that name is not informative or relevant. It is a templatized Circular Buffer and some tests. I am using the opaque class to test the data structure with full class/struct types. I am also testing with primitives (int at the moment) and that test function does not give me this compiler error. There is more code than this, but I've provided what I think are the relevant sections. The full code is here if you are interested.

gcc error:

tests.cpp: In function ‘bool opaque_fill_test(int)’:
tests.cpp:97:23: error: no matching function for call to ‘CircBuf<opaque>::remove(opaque (*)())’

tests.cpp:

struct opaque {
  int id;
  opaque(int n): id(n) {} 
  opaque operator=(const opaque &o) { this->id = o.id; }
};

opaque rcv();
CircBuf<opaque> c(size);
for (int i=0; i<size; i++) {
    if ( c.remove(&rcv)) {
        if (rcv.id != i ) {
            cout << "wrong value: " << rcv << " "
            << "should be: " << i << endl;
            return false;
        }
    } else {
        cout << "remove fail at i=" << rcv << endl;
        return false;
    }
}

CircBuf.h:

template<typename T> class CircBuf {
    ...
    template<typename T> bool CircBuf<T>::remove(T *t) {
       ...
       *t = data[front];
       ...

if i declare an opaque* and pass that to remove instead:

opaque rcv();
opaque* p = &rcv;
for (int i=0; i<size; i++) {
  if ( c.remove(p)) {
    ...
    ...

i get a similar error:

tests.cpp: In function ‘bool opaque_fill_test(int)’:
tests.cpp:96:16: error: cannot convert ‘opaque (*)()’ to ‘opaque*’ in initialization

Solution

  • If you have the declaration foo x();, the expression &x will actually be a function pointer assignable to something like foo *(y)(). The p variable in your last example isn't such a function pointer, but simply a pointer to an opaque struct. Thus you cannot assign the function pointer to that variable.

    [edit] Just remembered: maybe you meant to declare an opaque variable and initialize it using the default constructor. Leave out the parentheses then. It's a left-over syntax from C. You can only use the parentheses there if you actually put values in between to call a different constructor.