2011-10-20 18 views
1

我別無選擇,從那以後糾正,類似下面的代碼:是否存在用於隱含返回值的GCC擴展?

class SomeClass { 
public: 
    static int AdjustValue(float input); 
    static int DoSomethingWithAdjustedValue(int adjustedInput); 
    static int DoSomethingWithNormalValue(float input) { 
     DoSomethingWithAdjustedValue(AdjustValue(input);} 
}; 

所以DoSomethingWithNormalValue顯然缺少一個return,但GCC沒有產生錯誤或警告;我撥打DoSomethingWithNormalValue的地方獲得了正確的返回碼。我能想到的兩種解釋此:

  1. 有在GCC一些擴展,承認包裝是其他內聯函數的功能和一起傳遞的返回值。

  2. 由於未定義行爲的幸運/不幸表現與GCC中的錯誤相結合導致返回代碼被傳遞,從而避免了任何錯誤或警告。

這些都沒有可能。這裏發生了什麼?

+1

提高警戒級別。它會警告你。 –

+0

什麼是「錯誤返回代碼」? –

+0

@Blagovest我假設爲0. –

回答

3

嘗試:

> g++ xa1.cpp -Wall 
xa1.cpp: In static member function ‘static int SomeClass::DoSomethingWithNormalValue(float)’: 
xa1.cpp:8: warning: no return statement in function returning non-void 
xa1.cpp:8: warning: control reaches end of non-void function 

正在一起傳遞,因爲不確定的行爲與GCC,阻止任何錯誤或警告的錯誤結合的幸運/不幸的表現的返回碼。

我想你會發現這會發生什麼。
從技術上講,這是未定義的行爲。

+0

如果這是在x86上,他可能正在讀取'eax'的舊值,如果不被覆蓋,它將很幸運地保存前一個函數的返回碼。 –

+0

這是最殘酷的事情,我可以發誓我已經看到「控制達到非無效功能的結束」之前彈出。但我只是檢查了我的構建設置,你是對的。衛生署! – IronMensan

4

C++不需要診斷,但它是未定義的行爲。

你得到正確的值的原因是因爲DoSomethingWithAdjustedValue放在它的返回值,其中返回值去,然後DoSomethingWithNormalValue不與任何返回值搞亂,所以任何查找返回值他們稱之爲DoSomethingWithNormalValue後,將請參閱DoSomethingWithAdjustedValue返回的值。