Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385295

Explain GCC error after using an object accidentally declared as a function

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 ().

Upvotes: 2

Views: 187

Answers (2)

MSalters
MSalters

Reputation: 180030

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.

Upvotes: 2

etarion
etarion

Reputation: 17159

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

Upvotes: 5

Related Questions