模板代理方法无法编译



我在编译以下代码时遇到问题。

template<typename W, typename I, typename B>
class ConcreteInterfaceWrapper
{
protected:
template<typename... Args, void (W::*Functor)( Args... )>
static void proxyCall( void* object, Args... args ) { (static_cast<W*>( object )->Functor)( args... ); }
{
class Plugin: public ConcreteInterfaceWrapper<Plugin, IPlugin, IPluginBase>
{
public:
void tearDown() {}
void somethingOther() { proxyCall<&tearDown>( nullptr ); }
}

基本上,我正在尝试实现一个通用代理函数,该函数将允许我调用派生类的成员。我正在使用代理函数插入 C 结构的函数指针,因此无法更改proxyCall的签名。另一种方法是为每个方法创建一个代理函数,例如void proxyInitialize( void* object ) { static_cast<Derived1*>( object )->initialize(); }

我遇到了我的编译器 (g++( 抱怨没有匹配函数的问题proxyCall并且我得到了两个无用的注释:

note: candidate: template<class ... Args, void (Plugin::* Functor)(Args ...)> static void ConcreteInterfaceWrapper<W, I, B>::proxyCall(void*, Args ...) [with Args = {Args ...}; void (W::* Functor)(Args ...) = Functor; W = Plugin; I = IPlugin; B = IPluginBase]
static void proxyCall( void*, Args... );
note:   template argument deduction/substitution failed:

编译器无法推断出您在这种情况下Args...。下面是一个可能的解决方法:显式传递&tearDown的类型。

template <typename F, F FPtr, typename ...Args>
static void proxyCall( void* object, Args... args ) 
{ 
(static_cast<W*>( object )->FPtr)( args... ); 
}

void somethingOther() 
{ 
proxyCall<decltype(&tearDown), &tearDown>( nullptr ); 
}

请注意,在 C++17 中,您将能够执行以下操作:

template <auto FPtr, typename ...Args>
static void proxyCall( void* object, Args... args ) 
{ 
(static_cast<W*>( object )->FPtr)( args... ); 
}

void somethingOther() 
{ 
proxyCall<&tearDown>( nullptr ); 
}

最新更新