我基本上希望为通用 C 函数生成一个包装器,而无需手动指定类型。所以我有一个带有固定原型的回调,但我需要根据包装函数的类型在包装器中执行一些特殊代码......所以基本上我正在考虑在类模板中使用静态方法将我的函数包装到符合标准的接口,例如:
// this is what we want the wrapped function to look like
typedef void (*callback)(int);
void foobar( float x ); // wrappee
// doesn't compile
template< T (*f)(S) > // non-type template param, it's a function ptr
struct Wrapper
{
static void wrapped(int x)
{
// do a bunch of other stuff here
f(static_cast<S>(x)); // call wrapped function, ignore result
}
}
然后我想做类似的事情:
AddCallback( Wrapper<foobar>::wrapped );
但是,问题是我不能直接在 Wrapper 模板中的函数参数中使用“S”,我必须首先将其列为参数:
template< class T, class S, T (*f)(S) >
struct Wrapper
// ...
但这意味着使用 ( ) 会更痛苦Wrapper<void,float,foobar>::wrapped
,理想情况下,我只想在那里传递函数指针并让它自动计算出参数的类型(和返回类型)。需要明确的是,在包装函数内部,我需要引用函数指针的类型(所以我确实需要一些等价的 S 或 T)。
有没有办法做到这一点?