深入分析:C++模板究竟会使代码膨胀吗

今天和同事说到C++模板会使代码膨胀, 可同事觉得不会。
同事的依据是: 如果模板会使代码膨胀, 那么ATL和WTL里为什么还要大量使用模板? 同样功能 ,ATL和WTL编译出的可执行文件可比MFC编译的要小的多。
我当时一愣 ,事实确实如同事所说,难道模板会使代码膨胀的观点是错误的吗?

MFC因为本身代码量和复杂性在那里, 所以它生成比较大的exe无可厚非。我们这里重点关注为什么ATL/WTL使用模板,但是却不会使生成的exe变大。

我们知道使用模板时, 同一模板生成不同的模板实类后会是多份代码 ,比如 vector<int>, vector<char>, vector<double>, 这里总共会生成3份不同的vector代码,这就是我们平时所说的代码膨胀。

那么为什么ATL/WTL就没有代码膨胀的问题呢?
我这里以 ATL里的窗口代码为例来分析这个问题,因为我对WinDbg比较熟悉,下面我会以WinDbg为工具来分析我以前的写得那个俄罗斯方块程序。

首先我们看一下ATL的窗口代码:


代码如下:

template <class T, class TBase /* = CWindow */, class TWinTraits /* = CControlWinTraits */>
class ATL_NO_VTABLE CWindowImpl : public CWindowImplBaseT< TBase, TWinTraits >
{
public:
    DECLARE_WND_CLASS(NULL)

static LPCTSTR GetWndCaption()
    {
        return NULL;
    }

HWND Create(HWND hWndParent, _U_RECT rect = NULL, LPCTSTR szWindowName = NULL,
            DWORD dwStyle = 0, DWORD dwExStyle = 0,
            _U_MENUorID MenuOrID = 0U, LPVOID lpCreateParam = NULL)
    {
        if (T::GetWndClassInfo().m_lpszOrigName == NULL)
            T::GetWndClassInfo().m_lpszOrigName = GetWndClassName();
        ATOM atom = T::GetWndClassInfo().Register(&m_pfnSuperWindowProc);

dwStyle = T::GetWndStyle(dwStyle);
        dwExStyle = T::GetWndExStyle(dwExStyle);

// set caption
        if (szWindowName == NULL)
            szWindowName = T::GetWndCaption();

return CWindowImplBaseT< TBase, TWinTraits >::Create(hWndParent, rect, szWindowName,
            dwStyle, dwExStyle, MenuOrID, atom, lpCreateParam);
    }
};

上面是一个模板类,它应该会生成多份模板实例代码:我们可以用WinDbg的符号搜索命令来做验证:
输入 x HYTeris!ATL::CWindowImpl<* , 搜索所有以 HYTeris!ATL::CWindowImpl< 开头的符号


代码如下:

0:000> x HYTeris!ATL::CWindowImpl<*
004592f0 HYTeris!ATL::CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
004343a0 HYTeris!ATL::CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00437c90 HYTeris!ATL::CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
00430440 HYTeris!ATL::CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> >::CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> > (void)
0041c990 HYTeris!ATL::CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
0044cf50 HYTeris!ATL::CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
004539a0 HYTeris!ATL::CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
00435800 HYTeris!ATL::CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
00434640 HYTeris!ATL::CWindowImpl<CTsKeyEdit,WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsKeyEdit,WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > (void)
00437440 HYTeris!ATL::CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> >::GetWndCaption (void)
00436020 HYTeris!ATL::CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
00453f70 HYTeris!ATL::CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
00424e30 HYTeris!ATL::CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
004338a0 HYTeris!ATL::CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
0041c4f0 HYTeris!ATL::CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
00441b50 HYTeris!ATL::CWindowImpl<CTsKeyEdit,WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsKeyEdit,WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > (void)
004365f0 HYTeris!ATL::CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
00455120 HYTeris!ATL::CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > (void)
0040ea20 HYTeris!ATL::CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
00453fa0 HYTeris!ATL::CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
0041abc0 HYTeris!ATL::CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00459c20 HYTeris!ATL::CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
0045f640 HYTeris!ATL::CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
004608a0 HYTeris!ATL::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndClassInfo (void)
0045fed0 HYTeris!ATL::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
00433a70 HYTeris!ATL::CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00437c60 HYTeris!ATL::CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
0044fb70 HYTeris!ATL::CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
0041acf0 HYTeris!ATL::CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00459510 HYTeris!ATL::CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
00437a90 HYTeris!ATL::CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
00430490 HYTeris!ATL::CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> >::~CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> > (void)
00424de0 HYTeris!ATL::CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00433ac0 HYTeris!ATL::CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00433850 HYTeris!ATL::CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00433b60 HYTeris!ATL::CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00452050 HYTeris!ATL::CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
0044f8e0 HYTeris!ATL::CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
0044cec0 HYTeris!ATL::CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
004340e0 HYTeris!ATL::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00433ef0 HYTeris!ATL::CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > (void)
004348c0 HYTeris!ATL::CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::~CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > > (void)
004535b0 HYTeris!ATL::CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
00454170 HYTeris!ATL::CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
00459c70 HYTeris!ATL::CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00451ed0 HYTeris!ATL::CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00451f20 HYTeris!ATL::CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
0045c4b0 HYTeris!ATL::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00436320 HYTeris!ATL::CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
004536a0 HYTeris!ATL::CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
004604a0 HYTeris!ATL::CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::GetWndCaption (void)
0040eca0 HYTeris!ATL::CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
0045ff90 HYTeris!ATL::CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
00437cc0 HYTeris!ATL::CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
004366e0 HYTeris!ATL::CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
0045e620 HYTeris!ATL::CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
004608d0 HYTeris!ATL::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndCaption (void)
004520a0 HYTeris!ATL::CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
0045d210 HYTeris!ATL::CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > > (void)
004d84a8 HYTeris!ATL::CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d84b8 HYTeris!ATL::CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d7970 HYTeris!ATL::CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d7960 HYTeris!ATL::CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d914c HYTeris!ATL::CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d8dd0 HYTeris!ATL::CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d8b04 HYTeris!ATL::CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d8b14 HYTeris!ATL::CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d8ae8 HYTeris!ATL::CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004e31b8 HYTeris!ATL::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004e2980 HYTeris!ATL::CWindowImpl<CTsMainDisplayPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d88c4 HYTeris!ATL::CWindowImpl<CTsKeyEdit,WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d8330 HYTeris!ATL::CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d88f0 HYTeris!ATL::CWindowImpl<CTsKeyEdit,WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d88e0 HYTeris!ATL::CWindowImpl<CTsKeyEdit,WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d8a40 HYTeris!ATL::CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004e30f0 HYTeris!ATL::CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d8a5c HYTeris!ATL::CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d8a6c HYTeris!ATL::CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004e21d0 HYTeris!ATL::CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d8c70 HYTeris!ATL::CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004e2e78 HYTeris!ATL::CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004e1f00 HYTeris!ATL::CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> > `RTTI Type Descriptor' = <no type information>
004d9208 HYTeris!ATL::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d9218 HYTeris!ATL::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004e1ad8 HYTeris!ATL::CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d834c HYTeris!ATL::CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d835c HYTeris!ATL::CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d8c9c HYTeris!ATL::CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d8c8c HYTeris!ATL::CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d856c HYTeris!ATL::CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d855c HYTeris!ATL::CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004e2bb8 HYTeris!ATL::CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d7fa0 HYTeris!ATL::CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d7fb0 HYTeris!ATL::CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d91ec HYTeris!ATL::CWindowImpl<CTsSingleControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004e2048 HYTeris!ATL::CWindowImpl<CTsMainButtonPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d8174 HYTeris!ATL::CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d8184 HYTeris!ATL::CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> >::`RTTI Base Class Array' = <no type information>
004d8dfc HYTeris!ATL::CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d8dec HYTeris!ATL::CWindowImpl<CTsMutiScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004e2728 HYTeris!ATL::CWindowImpl<CTsKeyEdit,WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004e28c8 HYTeris!ATL::CWindowImpl<CTsNextElementContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d8fac HYTeris!ATL::CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d8f9c HYTeris!ATL::CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d848c HYTeris!ATL::CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d8f80 HYTeris!ATL::CWindowImpl<CTsButton,WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d8158 HYTeris!ATL::CWindowImpl<CTsGameMainWnd,ATL::CWindow,ATL::CWinTraits<114229248,262400> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d8540 HYTeris!ATL::CWindowImpl<CTsMainStatusPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d7944 HYTeris!ATL::CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d7f84 HYTeris!ATL::CWindowImpl<CTsDisplayContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004e2aa0 HYTeris!ATL::CWindowImpl<CTsSingleScoreContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d9168 HYTeris!ATL::CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d9178 HYTeris!ATL::CWindowImpl<CTsSubControlContainer,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004e1628 HYTeris!ATL::CWindowImpl<CTsAgentWindow,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004e2148 HYTeris!ATL::CWindowImpl<CTsMainControlPane,ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d95c4 HYTeris!ATL::CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::`RTTI Base Class Array' = <no type information>
004d95b4 HYTeris!ATL::CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::`RTTI Class Hierarchy Descriptor' = <no type information>
004e3400 HYTeris!ATL::CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > > `RTTI Type Descriptor' = <no type information>
004d9598 HYTeris!ATL::CWindowImpl<CTsUserListCtrl,WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>

上面的符号可以验证我们多份实例的猜想。
可以看到 CWindowImpl和我们的窗口继承类(T),窗口基类(TBase), 窗口风格(TWinTraits) 相关,所以数量很多 。  那么为什么这么多的符号,也就是这么多的函数代码 ,exe大小没有被撑大?
那时因为CWindowImpl类函数本身很小, 我们从函数的 代码量上就可以看出来。

用同样的方法 ,我们可以继续分析 CWindowImplBaseT


代码如下:

template <class TBase = CWindow, class TWinTraits = CControlWinTraits>
class ATL_NO_VTABLE CWindowImplBaseT : public CWindowImplRoot< TBase >
{
public:
    WNDPROC m_pfnSuperWindowProc;

CWindowImplBaseT() : m_pfnSuperWindowProc(::DefWindowProc)
    {}

static DWORD GetWndStyle(DWORD dwStyle)
    {
        return TWinTraits::GetWndStyle(dwStyle);
    }
    static DWORD GetWndExStyle(DWORD dwExStyle)
    {
        return TWinTraits::GetWndExStyle(dwExStyle);
    }

virtual WNDPROC GetWindowProc()
    {
        return WindowProc;
    }
    static LRESULT CALLBACK StartWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    HWND Create(HWND hWndParent, _U_RECT rect, LPCTSTR szWindowName,
            DWORD dwStyle, DWORD dwExStyle, _U_MENUorID MenuOrID, ATOM atom, LPVOID lpCreateParam = NULL);
    BOOL DestroyWindow()
    {
        ATLASSERT(::IsWindow(m_hWnd));
        return ::DestroyWindow(m_hWnd);
    }
    BOOL SubclassWindow(HWND hWnd);
    HWND UnsubclassWindow(BOOL bForce = FALSE);

LRESULT DefWindowProc()
    {
        const _ATL_MSG* pMsg = m_pCurrentMsg;
        LRESULT lRes = 0;
        if (pMsg != NULL)
            lRes = DefWindowProc(pMsg->message, pMsg->wParam, pMsg->lParam);
        return lRes;
    }

LRESULT DefWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
#ifdef STRICT
        return ::CallWindowProc(m_pfnSuperWindowProc, m_hWnd, uMsg, wParam, lParam);
#else
        return ::CallWindowProc((FARPROC)m_pfnSuperWindowProc, m_hWnd, uMsg, wParam, lParam);
#endif
    }

virtual void OnFinalMessage(HWND /*hWnd*/)
    {
        // override to do something, if needed
    }
};

代码如下:

0:000> x HYTeris!ATL::CWindowImplBaseT<*
0040f170 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, unsigned short, void *)
0040ec20 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndStyle (unsigned long)
00435970 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::OnFinalMessage (struct HWND__ *)
004604d0 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::GetWndStyle (unsigned long)
00434a10 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::~CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > (void)
0040ec60 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWndExStyle (unsigned long)
00461740 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::WindowProc (struct HWND__ *, unsigned int, unsigned int, long)
0044c040 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::WindowProc (struct HWND__ *, unsigned int, unsigned int, long)
004346e0 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::~CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > (void)
0041c440 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00435940 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::GetWindowProc (void)
00459580 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::GetWndExStyle (unsigned long)
00461950 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, unsigned short, void *)
004395b0 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::StartWindowProc (struct HWND__ *, unsigned int, unsigned int, long)
00439ea0 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::DefWindowProcW (unsigned int, unsigned int, long)
00461620 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::StartWindowProc (struct HWND__ *, unsigned int, unsigned int, long)
004596e0 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, unsigned short, void *)
0041ced0 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::WindowProc (struct HWND__ *, unsigned int, unsigned int, long)
0044c370 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::UnsubclassWindow (int)
00460510 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::GetWndExStyle (unsigned long)
00457f70 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::WindowProc (struct HWND__ *, unsigned int, unsigned int, long)
0045f830 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::GetWindowProc (void)
00457820 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > (void)
004374b0 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::GetWndExStyle (unsigned long)
00434ad0 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::~CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > > (void)
004595c0 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::StartWindowProc (struct HWND__ *, unsigned int, unsigned int, long)
004358f0 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> > (void)
00445920 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::OnFinalMessage (struct HWND__ *)
00461ad0 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::DefWindowProcW (unsigned int, unsigned int, long)
0041c4c0 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::OnFinalMessage (struct HWND__ *)
00437470 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::GetWndStyle (unsigned long)
004398e0 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::Create (struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, unsigned short, void *)
0040ed50 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::StartWindowProc (struct HWND__ *, unsigned int, unsigned int, long)
004458a0 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > (void)
0045f7e0 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > > (void)
0041ad30 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::~CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> > (void)
00430550 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::~CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> > (void)
0045f860 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::OnFinalMessage (struct HWND__ *)
004582a0 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::UnsubclassWindow (int)
0041c490 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::GetWindowProc (void)
0044cb50 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::DefWindowProcW (unsigned int, unsigned int, long)
0044c250 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::SubclassWindow (struct HWND__ *)
004458f0 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::GetWindowProc (void)
00459540 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::GetWndStyle (unsigned long)
00458180 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::SubclassWindow (struct HWND__ *)
00457870 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::GetWindowProc (void)
00457910 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::OnFinalMessage (struct HWND__ *)
0040e990 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::DestroyWindow (void)
004396d0 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::WindowProc (struct HWND__ *, unsigned int, unsigned int, long)
0041d1c0 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::DefWindowProcW (unsigned int, unsigned int, long)
004578a0 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::DefWindowProcW (unsigned int, unsigned int, long)
004d8fc8 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d8ff4 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d8fe4 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d79b4 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d79a4 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004e2798 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d819c HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d8938 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Array' = <no type information>
004d8928 HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d960c HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::`RTTI Base Class Array' = <no type information>
004d95fc HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::`RTTI Class Hierarchy Descriptor' = <no type information>
004e2ee8 HYTeris!ATL::CWindowImplBaseT<WTL::CButtonT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d81c8 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::`RTTI Base Class Array' = <no type information>
004d81b8 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004e34a0 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > > `RTTI Type Descriptor' = <no type information>
004d95e0 HYTeris!ATL::CWindowImplBaseT<WTL::CListViewCtrlT<ATL::CWindow>,ATL::CWinTraitsOR<32777,0,ATL::CWinTraits<1442840576,0> > >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004e1f68 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> > `RTTI Type Descriptor' = <no type information>
004e1688 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> > `RTTI Type Descriptor' = <no type information>
004d7988 HYTeris!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d890c HYTeris!ATL::CWindowImplBaseT<WTL::CEditT<ATL::CWindow>,ATL::CWinTraits<1442840576,0> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>

可以看到 CWindowImplBaseT只与 窗口基类和窗口风格相关 ,数量 已经减小很多 ,CWindowImplBaseT模板类本身函数数量不多 ,函数代码量大小也不大 。

然后再看CWindowImplRoot,


代码如下:

template <class TBase /* = CWindow */>
class ATL_NO_VTABLE CWindowImplRoot : public TBase, public CMessageMap
{
public:
    CWndProcThunk m_thunk;
    const _ATL_MSG* m_pCurrentMsg;
    DWORD m_dwState;

enum { WINSTATE_DESTROYED = 0x00000001 };

// Constructor/destructor
    CWindowImplRoot() : m_pCurrentMsg(NULL), m_dwState(0)
    { }

virtual ~CWindowImplRoot()
    {
#ifdef _DEBUG
        if(m_hWnd != NULL)    // should be cleared in WindowProc
        {
            ATLTRACE(atlTraceWindowing, 0, _T("ERROR - Object deleted before window was destroyed\n"));
            ATLASSERT(FALSE);
        }
#endif //_DEBUG
    }

// Current message
    const _ATL_MSG* GetCurrentMessage() const
    {
        return m_pCurrentMsg;
    }

// "handled" management for cracked handlers
    BOOL IsMsgHandled() const
    {
        const _ATL_MSG* pMsg = GetCurrentMessage();
        ATLASSERT(pMsg != NULL);
        ATLASSERT(pMsg->cbSize >= sizeof(_ATL_MSG));
        return pMsg->bHandled;
    }
    void SetMsgHandled(BOOL bHandled)
    {
        _ATL_MSG* pMsg = (_ATL_MSG*)GetCurrentMessage();    // override const
        ATLASSERT(pMsg != NULL);
        ATLASSERT(pMsg->cbSize >= sizeof(_ATL_MSG));
        pMsg->bHandled = bHandled;
    }

// Message forwarding and reflection support
    LRESULT ForwardNotifications(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT ReflectNotifications(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    static BOOL DefaultReflectionHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lResult);
};

代码如下:

0:000> x HYTeris!ATL::CWindowImplRoot*
004364d0 HYTeris!ATL::CWindowImplRoot<WTL::CListViewCtrlT<ATL::CWindow> >::~CWindowImplRoot<WTL::CListViewCtrlT<ATL::CWindow> > (void)
004367d0 HYTeris!ATL::CWindowImplRoot<WTL::CEditT<ATL::CWindow> >::~CWindowImplRoot<WTL::CEditT<ATL::CWindow> > (void)
00457c30 HYTeris!ATL::CWindowImplRoot<WTL::CButtonT<ATL::CWindow> >::CWindowImplRoot<WTL::CButtonT<ATL::CWindow> > (void)
00460550 HYTeris!ATL::CWindowImplRoot<WTL::CListViewCtrlT<ATL::CWindow> >::CWindowImplRoot<WTL::CListViewCtrlT<ATL::CWindow> > (void)
004479d0 HYTeris!ATL::CWindowImplRoot<WTL::CEditT<ATL::CWindow> >::CWindowImplRoot<WTL::CEditT<ATL::CWindow> > (void)
0041c360 HYTeris!ATL::CWindowImplRoot<ATL::CWindow>::~CWindowImplRoot<ATL::CWindow> (void)
00435f40 HYTeris!ATL::CWindowImplRoot<WTL::CButtonT<ATL::CWindow> >::~CWindowImplRoot<WTL::CButtonT<ATL::CWindow> > (void)
004613f0 HYTeris!ATL::CWindowImplRoot<ATL::CWindow>::ForwardNotifications (unsigned int, unsigned int, long, int *)
00438f50 HYTeris!ATL::CWindowImplRoot<ATL::CWindow>::ReflectNotifications (unsigned int, unsigned int, long, int *)
0041c8e0 HYTeris!ATL::CWindowImplRoot<ATL::CWindow>::CWindowImplRoot<ATL::CWindow> (void)
004e2800 HYTeris!ATL::CWindowImplRoot<WTL::CEditT<ATL::CWindow> > `RTTI Type Descriptor' = <no type information>
004d897c HYTeris!ATL::CWindowImplRoot<WTL::CEditT<ATL::CWindow> >::`RTTI Base Class Array' = <no type information>
004d896c HYTeris!ATL::CWindowImplRoot<WTL::CEditT<ATL::CWindow> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d9640 HYTeris!ATL::CWindowImplRoot<WTL::CListViewCtrlT<ATL::CWindow> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d9650 HYTeris!ATL::CWindowImplRoot<WTL::CListViewCtrlT<ATL::CWindow> >::`RTTI Base Class Array' = <no type information>
004d8950 HYTeris!ATL::CWindowImplRoot<WTL::CEditT<ATL::CWindow> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d9028 HYTeris!ATL::CWindowImplRoot<WTL::CButtonT<ATL::CWindow> >::`RTTI Class Hierarchy Descriptor' = <no type information>
004d9038 HYTeris!ATL::CWindowImplRoot<WTL::CButtonT<ATL::CWindow> >::`RTTI Base Class Array' = <no type information>
004e16dc HYTeris!ATL::CWindowImplRoot<ATL::CWindow> `RTTI Type Descriptor' = <no type information>
004e3530 HYTeris!ATL::CWindowImplRoot<WTL::CListViewCtrlT<ATL::CWindow> > `RTTI Type Descriptor' = <no type information>
004d9624 HYTeris!ATL::CWindowImplRoot<WTL::CListViewCtrlT<ATL::CWindow> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d79c8 HYTeris!ATL::CWindowImplRoot<ATL::CWindow>::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004e2f58 HYTeris!ATL::CWindowImplRoot<WTL::CButtonT<ATL::CWindow> > `RTTI Type Descriptor' = <no type information>
004d79f4 HYTeris!ATL::CWindowImplRoot<ATL::CWindow>::`RTTI Base Class Array' = <no type information>
004d79e4 HYTeris!ATL::CWindowImplRoot<ATL::CWindow>::`RTTI Class Hierarchy Descriptor' = <no type information>
004d900c HYTeris!ATL::CWindowImplRoot<WTL::CButtonT<ATL::CWindow> >::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>

可以看到 CWindowImplRoot只和窗口基类(CWindow)相关 ,所以 相关的符号就更少了。

最后我们再看一下CWindow:


代码如下:

0:000> x HYTeris!ATL::CWindow::*
004e1000 HYTeris!ATL::CWindow::rcDefault = struct tagRECT
00442a20 HYTeris!ATL::CWindow::DestroyWindow (void)
00425730 HYTeris!ATL::CWindow::InvalidateRect (struct tagRECT *, int)
00442340 HYTeris!ATL::CWindow::CenterWindow (struct HWND__ *)
00425850 HYTeris!ATL::CWindow::UpdateWindow (void)
0041c950 HYTeris!ATL::CWindow::CWindow (struct HWND__ *)
004391e0 HYTeris!ATL::CWindow::GetDlgItem (int)
004568a0 HYTeris!ATL::CWindow::SetWindowRgn (struct HRGN__ *, int)
00441d20 HYTeris!ATL::CWindow::GetWindowLongW (int)
00444350 HYTeris!ATL::CWindow::Create (wchar_t *, struct HWND__ *, class ATL::_U_RECT, wchar_t *, unsigned long, unsigned long, class ATL::_U_MENUorID, void *)
004391b0 HYTeris!ATL::CWindow::operator HWND__ * (void)
00459080 HYTeris!ATL::CWindow::EnableWindow (int)
00425140 HYTeris!ATL::CWindow::GetClientRect (struct tagRECT *)
00430c70 HYTeris!ATL::CWindow::SetWindowTextW (wchar_t *)
0040eb30 HYTeris!ATL::CWindow::GetWndClassName (void)
00456770 HYTeris!ATL::CWindow::MoveWindow (int, int, int, int, int)
00442ac0 HYTeris!ATL::CWindow::KillTimer (unsigned int)
00455fc0 HYTeris!ATL::CWindow::GetWindowTextW (wchar_t *, int)
0040ba20 HYTeris!ATL::CWindow::IsWindow (void)
00441db0 HYTeris!ATL::CWindow::GetParent (void)
004315b0 HYTeris!ATL::CWindow::SetWindowPos (struct HWND__ *, int, int, int, int, unsigned int)
00456810 HYTeris!ATL::CWindow::GetWindowRect (struct tagRECT *)
00431860 HYTeris!ATL::CWindow::GetStyle (void)
00455250 HYTeris!ATL::CWindow::Invalidate (int)
004318f0 HYTeris!ATL::CWindow::SetCapture (void)
00442cf0 HYTeris!ATL::CWindow::SetTimer (unsigned int, unsigned int, <function> *)
00431980 HYTeris!ATL::CWindow::ModifyStyle (unsigned long, unsigned long, unsigned int)
00443d10 HYTeris!ATL::CWindow::GetDlgItemTextW (int, wchar_t *, int)
0040b980 HYTeris!ATL::CWindow::SendMessageW (unsigned int, unsigned int, long)
00434be0 HYTeris!ATL::CWindow::ShowWindow (int)
004d7a38 HYTeris!ATL::CWindow::`RTTI Base Class Descriptor at (0,-1,0,64)' = <no type information>
004d7a04 HYTeris!ATL::CWindow::`RTTI Base Class Descriptor at (4,-1,0,64)' = <no type information>
004d7a20 HYTeris!ATL::CWindow::`RTTI Class Hierarchy Descriptor' = <no type information>
004d7a30 HYTeris!ATL::CWindow::`RTTI Base Class Array' = <no type information>

我们看到CWindow只有一份 , 并且函数数量相比AtlWin.h减少了很多,因为ATL是以源代码的方式提供的 ,所有没有用到的函数不会被编译到我们最终的可执行文件中 。

通过上面的分析 ,相信我们知道了为什么ATL/WTL大量使用模板,但是生成的exe还是这么小的原因 :
不是模板不会使代码膨胀,而是ATL/WTL在设计时就关注了这个问题 ,它避免了在可能生成很多模板实例的模板类中编写大量代码(有些拗口,不知道你有没有读懂^_^)

总结下 ,如果你想用模板,但是又不想 让自己最终的可执行文件变的很大,
有2种方式:
(1)你的模板类不会生成很多模板实例,这样写成模板类还有意义吗?
(2)你的模板类的代码量或是函数个数很少,你可以仿照ATL的方式把模板无关的东西用继承的方式逐层剥离,确保模板类的代码都是和模板参数相关的。

(0)

相关推荐

  • C++模板之特化与偏特化详解

    前言 说到C++模板,这个已经不是什么新东西了,自己在实际开发中也用过:对于C++模板特化和偏特化,对于别人来说,已经不是什么新东西了,但是对于我来说,的确是我的盲区,那天在群里讨论这个问题,自己对于这部分确实没有掌握,又联想到在<STL源码剖析>一书中,对于此也是有着介绍.所以,今天就对此进行详细的总结,以备后忘. C++模板 说到C++模板特化与偏特化,就不得不简要的先说说C++中的模板.我们都知道,强类型的程序设计迫使我们为逻辑结构相同而具体数据类型不同的对象编写模式一致的代码,而无法抽

  • C++标准模板库函数sort的那些事儿

    STL里面有个sort函数,可以直接对数组排序,复杂度为n*log2(n).sort()定义在在头文件<algorithm>中.sort函数是标准模板库的函数,已知开始和结束的地址即可进行排序,可以用于比较任何容器(必须满足随机迭代器),任何元素,任何条件,执行速度一般比qsort要快.另外,sort()是类属函数,可以用于比较任何容器,任何元素,任何条件. 具体事例如下:char ch[20]="sdasdacsdasdas";cout<<ch<<

  • C++可变参数的函数与模板实例分析

    本文实例展示了C++可变参数的函数与模板的实现方法,有助于大家更好的理解可变参数的函数与模板的应用,具体内容如下: 首先,所谓可变参数指的是函数的参数个数可变,参数类型不定的函数.为了编写能处理不同数量实参的函数,C++提供了两种主要的方法:如果所有的实参类型相同,可以传递一个名为initializer_list的标准库类型:如果实参的类型不同,我们可以编写可变参数模板.另外,C++还有一种特殊的省略符形参,可以用它传递可变数量的实参,不过这种一般只用于与C函数交互的接口程序. 一.可变参数函数

  • c++中typename和class的区别介绍

    相信学习C++的人对class这个关键字都非常明白,class用于定义类.在模板引入c++后,最初定义模板的方法为: template<class T>...... 在这里class关键字表明T是一个类型,后来为了避免class在这两个地方的使用可能给人带来混淆,所以引入了typename这个关键字.它的作用同class一样表明后面的符号为一个类型,这样在定义模板的时候就可以使用下面的方式了: template<typename T>...... 在模板定义语法中关键字class与

  • C++类模板与模板类深入详解

    1.在c++的Template中很多地方都用到了typename与class这两个关键字,而且有时候二者可以替换,那么是不是这两个关键字完全一样呢? 事实上class用于定义类,在模板引入c++后,最初定义模板的方法为:template<class T>,这里class关键字表明T是一个类型,后来为了避免class在这两个地方的使用可能给人带来混淆,所以引入了typename这个关键字,它的作用同class一样表明后面的符号为一个类型,这样在定义模板的时候就可以使用下面的方式了:      t

  • C++大数模板(推荐)

    分别使用C++中的运算符重载的方法来实现大数之间的数学运算,包括加法.减法.乘法.除法.n次方.取模.大小比较.赋值以及输入流.输出流的重载..并且使用这个大数模板,顺利AC了HDOJ上的1134这个题目的Catalan数计数问题..http://acm.hdu.edu.cn/showproblem.php?pid=1134大数模板的代码如下: 复制代码 代码如下: #include<iostream> #include<string> #include<iomanip>

  • 浅析C++中模板的那点事

    1.什么是模板 假设现在我们完成这样的函数,给定两个数x和y求式子x^2 + y^2 + x * y的值 .考虑到x和y可能是 int , float 或者double类型,那么我们就要完成三个函数: int fun(int x,int y);float fun(float x,float y);double fun(double x,double y); 并且每个fun函数内部所要完成的操作也是极其的相似.如下: 复制代码 代码如下: int fun(int x,int y){    int

  • C++模板特例化应用实例

    模板特例化是C++程序设计中一个非常重要的应用,本文就以实例形式对其进行分析,相信对大家进一步理解C++程序设计能够带来一定的帮助.具体内容如下: 首先,模板是C++中一个很重要的特性,写一份代码能用于多种数据类型(包括用户自定义类型).例如,STL的sort()函数可以用于多种数据类型的排序,类stack可以用作多种数据类型的栈.但是,如果我们想对特定的数据类型执行不同的代码(而不是通用模板)呢?这种情况下就可以使用模板特例化(template specialization). 一.函数模板特

  • C++函数模板与类模板实例解析

    本文针对C++函数模板与类模板进行了较为详尽的实例解析,有助于帮助读者加深对C++函数模板与类模板的理解.具体内容如下: 泛型编程(Generic Programming)是一种编程范式,通过将类型参数化来实现在同一份代码上操作多种数据类型,泛型是一般化并可重复使用的意思.泛型编程最初诞生于C++中,目的是为了实现C++的STL(标准模板库). 模板(template)是泛型编程的基础,一个模板就是一个创建类或函数的蓝图或公式.例如,当使用一个vector这样的泛型类型或者find这样的泛型函数

  • C++中函数模板的用法详细解析

    定义 我们知道函数的重载可以实现一个函数名多用,将功能相同或者类似函数用同一个名来定义.这样可以简化函数的调用形式,但是程序中,仍然需要分别定义每一个函数. C++提供的函数模板可以更加简化这个过程. 所谓函数模板实际上是建立一个通用函数,其涵涵素类型额形参类型不具体指定,用一个虚拟的类型来代表,这个通用函数就称为函数模板. 凡是函数体相同的函数都可以用这个模板来代替,不必定义多个函数,只需要在模板中定义一次即可.在调用函数时,系统会根据实参的类型来取代模板中的虚拟类型,从而实现了不同函数的功能

  • C++的template模板中class与typename关键字的区别分析

    在C++模板中,可以使用class或者typename来声明模板参数,那么这两个关键字有什么区别呢? 模板参数声明 对于模板参数声明,这两个参数没有区别,含义是一样的. template class Simple; template class Simple; 上面两行都是声明一个模板类Simple. 表明类型 假如我们有这样一段代码: template void add(const T &acontainer, T &sum) { T::const_iterator iter = con

  • C++关键字typename的深入理解

    问题:在下面的 template declarations(模板声明)中 class 和 typename 有什么不同? 复制代码 代码如下: template<class T> class Widget; // uses "class"template<typename T> class Widget; // uses "typename" 答案:没什么不同.在声明一个 template type parameter(模板类型参数)的时候,

  • 详解C++的模板中typename关键字的用法

    typename的使用场合 用处1, 用在模板定义里, 标明其后的模板参数是类型参数. 例如 template<typename T, typename Y> T foo(const T& t, const Y& y){//....}; templace<typename T> class CTest { private: T t; public: //... } 其实,这里最常用的是使用关键字class,而且二者功能完全相同,这里的class和定义类时的class

  • C++中的类模板详解及示例

    C++中的函数模板 对于类的声明来说,也有同样的问题.有时,有两个或多个类,其功能是相同的,仅仅是数据类型不同,如下面语句声明了一个类: 复制代码 代码如下: class Compare_int{ public:  Compare(int a,int b)  {   x=a;   y=b;  }   int max()  {   return (x>y)?x:y;  }  int min()  {   return (x<y)?x:y;  } private:  int x,y;}; 其作用是

随机推荐