使用可变模板参数传递成员函数指针是不明确的
Passing Member Function pointer using variadic template arguments is ambiguous
为什么 "TArgs" 在我的示例中不明确?
编译器应该知道唯一存在的函数签名
virtual CGame::NetCreatePlayer(CNetPeer* _pPeer, int _ObjectID, const CNetMessage& _ObjectData, bool _bAuthority);
并在此函数中推导出正确的 TArgs
:
template<typename... TArgs >
void INetInterface<TSubClass>::NetCall(void(TSubClass::*_pFunc)(CNetPeer*, TArgs...), CNetPeer* _pPeer, TArgs... _Params);
我想这样称呼它:
CNetMessage obj;
//...
NetCall(&CGame_Server::NetCreatePlayer, _pClient, 0, obj, true);
CGame_Server继承CGame.
编译器输出:
4> error C2782: 'void INetInterface<CGame>::NetCall(void (__thiscall CGame::* )(CNetPeer *,TArgs...),CNetPeer *,TArgs...)'
: template parameter 'TArgs' is ambiguous
4> NetInterface.h(82) : see declaration of INetInterface<CGame>::NetCall'
4> could be 'int, const CNetMessage&, bool'
4> or 'int, CNetMessage, bool'
不可能是'int, CNetMessage, bool'吧?
有办法解决这个问题吗?
我尝试转换为 const CNetMessage&
但奇怪的是这并没有帮助。
不,没有其他同名的成员函数。
NetCall(&CGame_Server::NetCreatePlayer, _pClient, 0, obj, true);
这个调用有两个地方可以推导出TArgs
:
- 根据成员函数指针的类型,编译器推导出
TArgs == int, const CNetMessage&, bool
- 编译器从参数
0, obj, true
推导出TArgs == int, CNetMessage, bool
结果冲突。要解决此问题,请使用 identity
技巧将第二个 TArgs
放入非推导上下文中:
template<class T> struct identity { using type = T; };
template<class T> using identity_t = typename identity<T>::type;
template<typename... TArgs >
void INetInterface<TSubClass>::NetCall(void(TSubClass::*_pFunc)(CNetPeer*, TArgs...),
CNetPeer* _pPeer, identity_t<TArgs>... params);
附带说明,_Params
是保留标识符,还有 _ObjectData
和 _ObjectID
;你应该重命名它们。
为什么 "TArgs" 在我的示例中不明确?
编译器应该知道唯一存在的函数签名
virtual CGame::NetCreatePlayer(CNetPeer* _pPeer, int _ObjectID, const CNetMessage& _ObjectData, bool _bAuthority);
并在此函数中推导出正确的 TArgs
:
template<typename... TArgs >
void INetInterface<TSubClass>::NetCall(void(TSubClass::*_pFunc)(CNetPeer*, TArgs...), CNetPeer* _pPeer, TArgs... _Params);
我想这样称呼它:
CNetMessage obj;
//...
NetCall(&CGame_Server::NetCreatePlayer, _pClient, 0, obj, true);
CGame_Server继承CGame.
编译器输出:
4> error C2782: 'void INetInterface<CGame>::NetCall(void (__thiscall CGame::* )(CNetPeer *,TArgs...),CNetPeer *,TArgs...)'
: template parameter 'TArgs' is ambiguous
4> NetInterface.h(82) : see declaration of INetInterface<CGame>::NetCall'
4> could be 'int, const CNetMessage&, bool'
4> or 'int, CNetMessage, bool'
不可能是'int, CNetMessage, bool'吧?
有办法解决这个问题吗?
我尝试转换为 const CNetMessage&
但奇怪的是这并没有帮助。
不,没有其他同名的成员函数。
NetCall(&CGame_Server::NetCreatePlayer, _pClient, 0, obj, true);
这个调用有两个地方可以推导出TArgs
:
- 根据成员函数指针的类型,编译器推导出
TArgs == int, const CNetMessage&, bool
- 编译器从参数
0, obj, true
推导出TArgs == int, CNetMessage, bool
结果冲突。要解决此问题,请使用 identity
技巧将第二个 TArgs
放入非推导上下文中:
template<class T> struct identity { using type = T; };
template<class T> using identity_t = typename identity<T>::type;
template<typename... TArgs >
void INetInterface<TSubClass>::NetCall(void(TSubClass::*_pFunc)(CNetPeer*, TArgs...),
CNetPeer* _pPeer, identity_t<TArgs>... params);
附带说明,_Params
是保留标识符,还有 _ObjectData
和 _ObjectID
;你应该重命名它们。