简体   繁体   English

如何将成员声明为指向extern“C”函数的指针?

[英]How to declare a member as a pointer to an extern “C” function?

I'm looking to declare the type of an extern "C" function pointer. 我想要声明一个extern“C”函数指针的类型。 It is a member variable. 它是一个成员变量。 The syntax in this question I cannot get to compile. 这个问题的语法我无法编译。

template<typename Sig> struct extern_c_fp {
    extern "C" typedef typename std::add_pointer<Sig>::type func_ptr_type;
};

I have experimented with placing the extern "C" at both ends, and between typedef and typename and between type and func_ptr_type , but the compiler rejected all. 我已尝试在两端放置extern "C" ,在typedeftypename之间以及typefunc_ptr_type之间放置extern "C" ,但编译器拒绝了所有。 Any suggestions? 有什么建议么?

extern "C" {
    template<typename R, typename... Args>
    using extern_c_fp = R(*)(Args...);
}

using my_function_ptr = extern_c_fp<void, int, double>;
// returns void, takes int and double

This doesn't use the same interface as you use, but there may be a way to extract the return type and argument types of Sig . 这不使用与您使用的相同的接口,但可能有一种方法来提取Sig的返回类型和参数类型。

This works in clang 3.1. 这适用于clang 3.1。 Xeo pointed out it didn't work in GCC . Xeo指出它在海湾合作委员会中没有用 I'm not sure if this is a bug in either compiler, so be careful when using this. 我不确定这是否是任何编译器中的错误,因此在使用它时要小心。

You cannot declare a typedef like that (from 7.5p4): 你不能声明类似的typedef(从7.5p4开始):

A linkage-specification shall occur only in namespace scope (3.3). 链接规范只应在命名空间范围内发生(3.3)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM