當編譯器無法內聯函數時,gcc的inline __attribute__((__always_inline__))
是否會生成警告?gcc「未內聯」警告
因爲VS不http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx:
If the compiler cannot inline a function declared with __forceinline,
it generates a level 1 warning.
當編譯器無法內聯函數時,gcc的inline __attribute__((__always_inline__))
是否會生成警告?gcc「未內聯」警告
因爲VS不http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx:
If the compiler cannot inline a function declared with __forceinline,
it generates a level 1 warning.
你需要-Winline
以獲取有關非內聯函數的警告。
如果你想驗證這一點,你可以嘗試使用內聯函數的地址(這可以防止它被內聯),然後你應該看到一個警告。
#include <stdio.h>
static inline __attribute__ ((always_inline)) int add(int a, int b)
{
return a + b;
}
int main(void)
{
printf("%d\n", add(21, 21));
printf("%p\n", add);
return 0;
}
編輯
我一直在試圖產生與上面的代碼和其它的例子,但沒有成功警告 - 這似乎是gcc和鏗鏘的當前版本的行爲可能在這個已經改變區。如果我無法使用生成警告的更好示例進行編碼,我將刪除此答案。
一些更多的細節可能是必要的。我不能只按照這些說明提出警告。嘗試遞歸確實會產生警告,但它不依賴於傳遞'-Winline'。 – Potatoswatter
在這裏,工作示例http://coliru.stacked-crooked.com/a/d83ed9ae369590ce。只有在使用虛擬功能或其他間接呼叫(不能用普通呼叫扣除時)時,您可以使用always_inline獲得警告。簡單的'inline',來自另一隻手 - 實際上只是編譯器提示。 – tower120
很酷 - 我認爲用舊版本的gcc(即使是C代碼)得到這個警告要容易得多。如果你喜歡,隨意編輯你的代碼到我的答案。 –
您需要'-Winline' –
我添加了該標誌 - 但沒有任何反應。它僅在不能內聯時發出警告?我可以在某處閱讀嗎? – tower120
你可以測試它的功能,以防止它被內聯,然後你應該看到一個警告, –