Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to typedef a pointer-to-extern-"C"-function type within a template?

I want to add a public typedef to a template for a pointer to a function-taking-one-argument that uses "C" language linkage.

I tried:

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

And:

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

And:

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

without success.

Is what I am trying to accomplish possible?

like image 892
Daniel Trebbien Avatar asked Feb 03 '23 22:02

Daniel Trebbien


2 Answers

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.

Unfortunately, you simply can't do this in current C++. This text is unchanged in the latest C++0x draft, but "template typedefs" may be able to accomplish it.

like image 99
Fred Nurk Avatar answered Feb 05 '23 18:02

Fred Nurk


Consider typedef of a boost::function or the STL function objects ... also you can't define a template inside a extern "C" block for some quite obvious reasons if you think about it.

like image 45
AJG85 Avatar answered Feb 05 '23 18:02

AJG85