是否可以定义一个可以干净编译但在使用时会产生警告消息的 C++ 类方法,其方式类似于未实现的 C 库函数?就像是:
int MyClass::frobnicate()
{
link_warning(frobnicate, "function not implemented");
return 0;
}
一个__attribute__((deprecated))
或更新的[[deprecated]]
属性具有正确的功能(如果没有其他选项,我将使用该功能),但我想避免告诉用户frobnicate
由于 XYZ 原因实际上无法实现它时已弃用。
AC 代码示例(来自glibc):
#define link_warning(symbol, msg) \
__make_section_unallocated (".gnu.warning." #symbol) \
static const char __evoke_link_warning_##symbol[] \
__attribute__ ((used, section (".gnu.warning." #symbol __sec_comment))) \
= msg;
该宏给出的警告如下所示(这是使用它的代码示例):
/path/to/file.o:在函数“符号”中:
/path/to/file.c:line: 警告:味精
天真link_warning(MyClass::frobnicate, "function not implemented")
的似乎不起作用,查找和使用修饰名称似乎很笨拙且不可移植(我不确定它是否真的有效)。