开发者

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 actu开发者_如何学Cally 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 ().


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜