2013-03-20 51 views
16

的意義對於下面的代碼:方法的聲明改變符號

struct foo {}; 

struct A 
{ 
    typedef foo foo_type; 

    void foo(); 
}; 

GCC給出一個編譯器錯誤:

test.cpp:7:14: error: declaration of 'void A::foo()' [-fpermissive] 
    void foo(); 
      ^
test.cpp:1:8: error: changes meaning of 'foo' from 'struct foo' [-fpermissive] 
struct foo {}; 
     ^

但鐺接受它,沒有編譯器錯誤。誰是對的?

請注意,如果typedef被刪除或更改爲typedef ::foo foo_type,那麼gcc和clang都會接受該代碼。

+0

真正的問題是:*爲什麼*是Clang總是對的? – 2013-03-20 23:57:01

+1

當沒有typedef時,它並不奇怪 – 4pie0 2013-03-20 23:57:12

+0

@ cf16:我沒有說這是。我只想指出,儘管錯誤沒有提及typedef,但typedef與錯誤有關。 – HighCommander4 2013-03-20 23:59:16

回答

17

GCC是正確的,但不要求鐺給診斷(3.3.7):

A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.

這是因爲類範圍是如何工作的。的void foo();foo是類A整個範圍內的可見光,因此void foo();聲明從指struct foo的功能foo的名字改變了的typedef foo意義。