有没有办法检查extern "C"在编译时是否使用C-linkage(即with )声明给定的函数?
我正在开发一个插件系统.每个插件都可以为插件加载代码提供工厂功能.但是,这必须通过名称(以及随后使用GetProcAddress或dlsym)来完成.这要求使用C-linkage声明函数,以防止名称错位.如果使用C++声明引用函数 - 链接(而不是在运行时在具有该名称的函数不存在时查找),则能够抛出编译器错误会很好.
这是我的意思的简化示例:
extern "C" void my_func()
{
}
void my_other_func()
{
}
// Replace this struct with one that actually works
template<typename T>
struct is_c_linkage
{
static const bool value = true;
};
template<typename T>
void assertCLinkage(T *func)
{
static_assert(is_c_linkage<T>::value, "Supplied function does not have C-linkage");
}
int main()
{
assertCLinkage(my_func); // Should compile
assertCLinkage(my_other_func); // Should NOT compile
}
Run Code Online (Sandbox Code Playgroud)
是否有可能的实现is_c_linkage会为第二个函数抛出编译器错误,但不是第一个?我不确定它是否可能(尽管它可能作为编译器扩展存在,我仍然想知道).谢谢.