2

The following is a common typo with language newcomers, who think that they are defining an object but are actually declaring a function:

struct T
{
   void foo() {}
};

int main()
{
   T obj();
   obj.foo();
}

GCC 4.1.2's error is:

In function 'int main()':
Line 9: error: request for member 'foo' in 'obj', which is of non-class type 'T ()()'
compilation terminated due to -Wfatal-errors.

Why is the reported type in the message T ()()? I'd have expected T ().

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

2 Answers2

5

IIRC this is just a compiler bug. GCC 4.4 says T() while 4.2 says T()() for me.

etarion
  • 16,935
  • 4
  • 43
  • 66
2

The error is best understood when you realize that you usually don't write out function types without naming at least the function, but it's a bit more common for function pointers.

For instance, int (*fooPtr)() names the pointer. If you omit the name, you have int (*)(). Now, going from function pointer to function type would give you int ()().

There's no real standard here, because ISO C++ doesn't define canonical names for all types. For instance, const volatile int is the same type as volatile const int, and neither form is canonical.

MSalters
  • 173,980
  • 10
  • 155
  • 350