I've tried to use templates with wxWidgets and I have a problem.
I defined a class template in a header file (SControlResize.h), like:
Code: Select all
template <
typename T
>
class SControlResizer : public T
{
DECLARE_EVENT_TABLE()
public:
// Mouse left down event
void OnMouseLeftDown(wxMouseEvent& event)
{
event.Skip();
}
// Mouse left up event
void OnMouseLeftUp(wxMouseEvent& event)
{
event.Skip();
}
// Mouse move event
void OnMouseMove(wxMouseEvent& event)
{
event.Skip();
}
};
Code: Select all
BEGIN_EVENT_TABLE_TEMPLATE1(SControlResizer, T, T)
EVT_MOTION(SControlResizer::OnMouseMove)
EVT_LEFT_UP(SControlResizer::OnMouseLeftUp)
EVT_LEFT_DOWN(SControlResizer::OnMouseLeftDown)
END_EVENT_TABLE()
SControlResizer.cpp
D:\wx\Work\testApp60\GUI\wx\Control\SControlResizer.cpp(24) : error C2321: syntax error : unexpected 'SControlResizer<T>::sm_eventTable'
D:\wx\Work\testApp60\GUI\wx\Control\SControlResizer.cpp(24) : error C2063: 'sm_eventHashTable' : not a function
D:\wx\Work\testApp60\GUI\wx\Control\SControlResizer.cpp(24) : error C2040: 'sm_eventHashTable' : 'class wxEventHashTable (void)' differs in levels of indirection from 'class wxEventHashTable'
D:\wx\Work\testApp60\GUI\wx\Control\SControlResizer.cpp(24) : error C2954: template definitions cannot nest
I found out that the problem is in the BEGIN_EVENT_TABLE_TEMPLATE1 macro (wx\event.h) at the following line:
template<typename T1> \
wxEventHashTable theClass<T1>::sm_eventHashTable(theClass<T1>::sm_eventTable); \
and if I change the initialization of 'sm_eventHashTable' variable to use asignment operator instead of parameterized constructor, it works:
template<typename T1> \
wxEventHashTable theClass<T1>::sm_eventHashTable=theClass<T1>::sm_eventTable; \
Do you know why this happens ? How can I fix it without changing the source code (I'm afraid I'll break compatibility with future versions) ?
I use Microsoft Visual C++ 6.0, wxWidgets 2.8.3 debug unicode, windows XP.
Thanks.