如何編寫調用goto END_標籤的宏?在C中的宏預處理中生成標籤
對於前:
#define MY_MACRO() \
//How to define goto END_##function_name label??
my_function()
{
MY_MACRO();
END_my_function:
return;
}
的MY_MACRO應該簡單地用線
goto END_my_function;
如何編寫調用goto END_標籤的宏?在C中的宏預處理中生成標籤
對於前:
#define MY_MACRO() \
//How to define goto END_##function_name label??
my_function()
{
MY_MACRO();
END_my_function:
return;
}
的MY_MACRO應該簡單地用線
goto END_my_function;
我不認爲這是可以做到替換。儘管一些編譯器定義了__FUNCTION__
或__func__
,但這些宏並未在宏中擴展。
但是,請注意,您不需要每個功能都有單獨的標籤:您可以對所有功能使用END
,並且只需編寫goto END
即可。
'__func__'是C99,這是一個字符串,而不是標識符。 – bstpierre 2010-07-23 12:48:07
是啊!我最終這樣做了! :) – Bharathwaaj 2010-07-23 12:48:44
#define MY_MACRO(function_name) END_ ## function_name
這一點似乎是爲了避免顯式給出'function_name'。 – lhf 2010-07-23 12:45:53
Doh!我現在看到了,對不起。 – Praetorian 2010-07-23 12:48:29
預處理不知道什麼功能是在但你可以親近 - 你必須在函數名傳遞給這個宏
#include <stdio.h>
#define GOTO_END(f) goto f ## _end
void foo(void)
{
printf("At the beginning.\n");
GOTO_END(foo);
printf("You shouldn't see this.\n");
foo_end:
printf("At the end.\n");
return;
}
int main()
{
foo();
}
我不認爲這是可能的... – kennytm 2010-07-23 12:31:18