C++确定成员函数指针模板常量的类型
下面的代码片段编译&在 Visual Studio 2010 中为我工作。
现在我想改进一件小事,但我就是找不到方法。我尝试了一些模板模板技巧等,但无济于事。
我想要以下代码行:
Sender.AttachListener<XSelectionChanged, TestListener, &TestListener::OnSelectionChanged>(Listener);
看起来像
Sender.AttachListener<&TestListener::OnSelectionChanged>(Listener);
也就是说,我向 AttachListener 模板提供常量“void (TClass::*TMethod)(TEvent& _rEvent)”。我想从这个常量的类型中获取 TClass 和 TEvent 类型。
这可能吗?如果是,怎么办?
-马蒂亚斯
代码:
#include <stdio.h>
#include <tchar.h>
#include <map>
#include <iostream>
struct XEvent
{};
struct XSelectionChanged : public XEvent
{};
struct XValueChanged : public XEvent
{};
template<typename TEvent, typename TClass>
struct TMethodType
{
typedef void (TClass::*MethodType)(TEvent& _rEvent);
};
template<typename TEvent, typename TClass, void (TClass::*TMethod)(TEvent& _rEvent)>
struct TBoundMethod
{
static void Dispatch(TEvent& _rEvent, TClass* _pInstance)
{
(_pInstance->*TMethod)(_rEvent);
}
};
class CEventHost
{
public:
template <typename TEvent, typename TClass, void (TClass::*TMethod)(TEvent& _rEvent)>
void AttachListener(TClass& _rInstance)
{
TBoundMethod<TEvent, TClass, TMethod>::Dispatch( TEvent(), &_rInstance );
//m_EventHost.Attach( &TBoundMethod<TClass, TEvent, TMethod>::Dispatch, &_rInstance );
}
template <typename TEvent>
void SendEvent(TEvent& _rEvent)
{
//m_EventHost.Send( _rEvent );
}
protected:
//SHost m_EventHost;
};
class TestListener
{
public:
void OnSelectionChanged(XSelectionChanged& _rEvent)
{
printf("Selection changed!\n");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CEventHost Sender, Sender2;
TestListener Listener;
Sender.AttachListener<XSelectionChanged, TestListener, &TestListener::OnSelectionChanged>(Listener);
Sender.SendEvent(XSelectionChanged());
Sender2.SendEvent(XSelectionChanged());
Sender.SendEvent(XValueChanged());
getchar();
return 0;
}
The piece of code below compiles & works for me in Visual Studio 2010.
Now I'd like to improve 1 little thing, but I just can't find a way. I tried some template template trickery etc, but to no avail.
I want the following line of code:
Sender.AttachListener<XSelectionChanged, TestListener, &TestListener::OnSelectionChanged>(Listener);
to look like
Sender.AttachListener<&TestListener::OnSelectionChanged>(Listener);
That is, I feed a constant "void (TClass::*TMethod)(TEvent& _rEvent)" to the AttachListener template. From the type of this constant I want to get the TClass and TEvent type.
Is this possible? If yes, how?
-Matthias
Code:
#include <stdio.h>
#include <tchar.h>
#include <map>
#include <iostream>
struct XEvent
{};
struct XSelectionChanged : public XEvent
{};
struct XValueChanged : public XEvent
{};
template<typename TEvent, typename TClass>
struct TMethodType
{
typedef void (TClass::*MethodType)(TEvent& _rEvent);
};
template<typename TEvent, typename TClass, void (TClass::*TMethod)(TEvent& _rEvent)>
struct TBoundMethod
{
static void Dispatch(TEvent& _rEvent, TClass* _pInstance)
{
(_pInstance->*TMethod)(_rEvent);
}
};
class CEventHost
{
public:
template <typename TEvent, typename TClass, void (TClass::*TMethod)(TEvent& _rEvent)>
void AttachListener(TClass& _rInstance)
{
TBoundMethod<TEvent, TClass, TMethod>::Dispatch( TEvent(), &_rInstance );
//m_EventHost.Attach( &TBoundMethod<TClass, TEvent, TMethod>::Dispatch, &_rInstance );
}
template <typename TEvent>
void SendEvent(TEvent& _rEvent)
{
//m_EventHost.Send( _rEvent );
}
protected:
//SHost m_EventHost;
};
class TestListener
{
public:
void OnSelectionChanged(XSelectionChanged& _rEvent)
{
printf("Selection changed!\n");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CEventHost Sender, Sender2;
TestListener Listener;
Sender.AttachListener<XSelectionChanged, TestListener, &TestListener::OnSelectionChanged>(Listener);
Sender.SendEvent(XSelectionChanged());
Sender2.SendEvent(XSelectionChanged());
Sender.SendEvent(XValueChanged());
getchar();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,使用元函数来提取 TClass 和 TEvent,如下所示:
警告:未经测试;-)
Yes, use a meta-function to extract TClass and TEvent, something like this:
Warning: untested ;-)
您确实需要
std::function
或boost::function
。You need
std::function
orboost::function
, really.