Puppy
Puppy

Reputation: 146940

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. 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. Any suggestions?

Upvotes: 7

Views: 398

Answers (2)

user142019
user142019

Reputation:

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.

This works in clang 3.1. Xeo pointed out it didn’t work in GCC. I’m not sure if this is a bug in either compiler, so be careful when using this.

Upvotes: 1

Jesse Good
Jesse Good

Reputation: 52365

You cannot declare a typedef like that (from 7.5p4):

A linkage-specification shall occur only in namespace scope (3.3).

Upvotes: 0

Related Questions