c++ - 是否可以在模板中定义指向外部 -"C"函数类型的指针?

标签 c++ templates typedef extern

我想将一个公共(public) typedef 添加到一个模板,用于指向使用“C”语言链接的函数接受一个参数的指针。

我试过:

extern "C" {
    template <typename return_t_, typename arg1_t_>
    struct test
    {
        typedef return_t_ (*C_fun1_t)(arg1_t_);
    };
}

和:

template <typename return_t_, typename arg1_t_>
struct test
{
    extern "C" {
        typedef return_t_ (*C_fun1_t)(arg1_t_);
    }
};

和:

template <typename return_t_, typename arg1_t_>
struct test
{
    extern "C" typedef return_t_ (*C_fun1_t)(arg1_t_);
};

没有成功。

我正在努力实现的目标是否可行?

最佳答案

C++03,§7.5p4:

A linkage-specification shall occur only in namespace scope. … A C language linkage is ignored for the names of class members and the member function type of class member functions.

不幸的是,您根本无法在当前的 C++ 中执行此操作。此文本在最新的 C++0x 草案中未更改,但“模板类型定义”可能能够完成它。

关于c++ - 是否可以在模板中定义指向外部 -"C"函数类型的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4866433/

相关文章:

函数的 C typedef,如何使用 - 简单示例

c++ - vector push_back 崩溃

c++ - 编译错误

templates - Angular SubViews/多区域模板、指令和动态模板 URL

c - typedef 语句的不同命名方式的目的是什么?

c++ - 带有函数指针 : Function does not Exist 的 TypeDef

c++ - 模板链接错误

c++ - 从 QWidgetList (QList<QWidget*>) 访问 QWidget 子槽/信号

c++ - 如何使用整数模板参数实例化模板类的静态成员?

c++ - 为什么(或何时)模板引入其命名空间?