Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MFC macros with templates

Tags:

c++

templates

mfc

Is it possible to derive and use a C++ template class from a MFC class such as CDialog. I've tried, but the implementation falls over with the MFC macros used for message routing. For example;

template<class TYPE, class ARG_TYPE>
class CMyDialogT : public CDialog
{
public:
    CMyDialogT(CMyContainerT<TYPE,ARG_TYPE> *pData,CWnd* pParent = NULL);  
    CMyContainerT<TYPE,ARG_TYPE> *m_pData;
    // Generated message map functions
    //{{AFX_MSG(CMyDialogT)
    afx_msg void OnUpdateMyControl();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

template<class TYPE, class ARG_TYPE>
CMyDialogT<TYPE,ARG_TYPE>::CMyDialogT(CMyContainerT<TYPE,ARG_TYPE> *pData,CWnd* pParent)
    : CDialog(CMyDialogT::IDD, pParent)
{
    m_pData = pData;
}

BEGIN_MESSAGE_MAP(CGlobalEditT<TYPE,ARG_TYPE>, CDialog)
    //{{AFX_MSG_MAP(CGlobalEditT)
    ON_EN_UPDATE(IDC_MY_CONTROL, OnUpdateMyControl)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

The above fails to compile with a sequence of messages starting as follows;

 warning C4002: too many actual parameters for macro 'BEGIN_MESSAGE_MAP'
 error C2653: 'TYPE' : is not a class or namespace name

Is there any workaround for this, other than manually unrolling the MFC macros? I can't use template specialisation at this point, as given in a similar question here as I don't know all the possible values of TYPE and ARG_TYPE.

Another way of looking at the question would be 'can I embed a template class in another class without either specialising the template or making the host class a template class'. I can't answer this one either, I suspect the answer may be No.

Edit Partial solution for single type templates on MSDN here

like image 566
SmacL Avatar asked Dec 22 '22 04:12

SmacL


1 Answers

You have to use BEGIN_TEMPLATE_MESSAGE_MAP instead of BEGIN_MESSAGE_MAP.

like image 151
Javier De Pedro Avatar answered Dec 24 '22 03:12

Javier De Pedro