深入分析:C++模板究竟会使代码膨胀吗
本文导语: 今天和同事说到C++模板会使代码膨胀, 可同事觉得不会。同事的依据是: 如果模板会使代码膨胀, 那么ATL和WTL里为什么还要大量使用模板? 同样功能 ,ATL和WTL编译出的可执行文件可比MFC编译的要小的多。我当时一愣 ,事实确实如...
今天和同事说到C++模板会使代码膨胀, 可同事觉得不会。
同事的依据是: 如果模板会使代码膨胀, 那么ATL和WTL里为什么还要大量使用模板? 同样功能 ,ATL和WTL编译出的可执行文件可比MFC编译的要小的多。
我当时一愣 ,事实确实如同事所说,难道模板会使代码膨胀的观点是错误的吗?
MFC因为本身代码量和复杂性在那里, 所以它生成比较大的exe无可厚非。我们这里重点关注为什么ATL/WTL使用模板,但是却不会使生成的exe变大。
我们知道使用模板时, 同一模板生成不同的模板实类后会是多份代码 ,比如 vector, vector, vector, 这里总共会生成3份不同的vector代码,这就是我们平时所说的代码膨胀。
那么为什么ATL/WTL就没有代码膨胀的问题呢?
我这里以 ATL里的窗口代码为例来分析这个问题,因为我对WinDbg比较熟悉,下面我会以WinDbg为工具来分析我以前的写得那个俄罗斯方块程序。
首先我们看一下ATL的窗口代码:
template
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 x HYTeris!ATL::CWindowImpl
{
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::CWindowImplBaseTcbSize >= 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::~CWindowImplRoot (void)
004367d0 HYTeris!ATL::CWindowImplRoot::~CWindowImplRoot (void)
00457c30 HYTeris!ATL::CWindowImplRoot::CWindowImplRoot (void)
00460550 HYTeris!ATL::CWindowImplRoot::CWindowImplRoot (void)
004479d0 HYTeris!ATL::CWindowImplRoot::CWindowImplRoot (void)
0041c360 HYTeris!ATL::CWindowImplRoot::~CWindowImplRoot (void)
00435f40 HYTeris!ATL::CWindowImplRoot::~CWindowImplRoot (void)
004613f0 HYTeris!ATL::CWindowImplRoot::ForwardNotifications (unsigned int, unsigned int, long, int *)
00438f50 HYTeris!ATL::CWindowImplRoot::ReflectNotifications (unsigned int, unsigned int, long, int *)
0041c8e0 HYTeris!ATL::CWindowImplRoot::CWindowImplRoot (void)
004e2800 HYTeris!ATL::CWindowImplRoot `RTTI Type Descriptor' =
004d897c HYTeris!ATL::CWindowImplRoot::`RTTI Base Class Array' =
004d896c HYTeris!ATL::CWindowImplRoot::`RTTI Class Hierarchy Descriptor' =
004d9640 HYTeris!ATL::CWindowImplRoot::`RTTI Class Hierarchy Descriptor' =
004d9650 HYTeris!ATL::CWindowImplRoot::`RTTI Base Class Array' =
004d8950 HYTeris!ATL::CWindowImplRoot::`RTTI Base Class Descriptor at (0,-1,0,64)' =
004d9028 HYTeris!ATL::CWindowImplRoot::`RTTI Class Hierarchy Descriptor' =
004d9038 HYTeris!ATL::CWindowImplRoot::`RTTI Base Class Array' =
004e16dc HYTeris!ATL::CWindowImplRoot `RTTI Type Descriptor' =
004e3530 HYTeris!ATL::CWindowImplRoot `RTTI Type Descriptor' =
004d9624 HYTeris!ATL::CWindowImplRoot::`RTTI Base Class Descriptor at (0,-1,0,64)' =
004d79c8 HYTeris!ATL::CWindowImplRoot::`RTTI Base Class Descriptor at (0,-1,0,64)' =
004e2f58 HYTeris!ATL::CWindowImplRoot `RTTI Type Descriptor' =
004d79f4 HYTeris!ATL::CWindowImplRoot::`RTTI Base Class Array' =
004d79e4 HYTeris!ATL::CWindowImplRoot::`RTTI Class Hierarchy Descriptor' =
004d900c HYTeris!ATL::CWindowImplRoot::`RTTI Base Class Descriptor at (0,-1,0,64)' =
可以看到 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, *)
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)' =
004d7a04 HYTeris!ATL::CWindow::`RTTI Base Class Descriptor at (4,-1,0,64)' =
004d7a20 HYTeris!ATL::CWindow::`RTTI Class Hierarchy Descriptor' =
004d7a30 HYTeris!ATL::CWindow::`RTTI Base Class Array' =
我们看到CWindow只有一份 , 并且函数数量相比AtlWin.h减少了很多,因为ATL是以源代码的方式提供的 ,所有没有用到的函数不会被编译到我们最终的可执行文件中 。
通过上面的分析 ,相信我们知道了为什么ATL/WTL大量使用模板,但是生成的exe还是这么小的原因 :
不是模板不会使代码膨胀,而是ATL/WTL在设计时就关注了这个问题 ,它避免了在可能生成很多模板实例的模板类中编写大量代码(有些拗口,不知道你有没有读懂^_^)
总结下 ,如果你想用模板,但是又不想 让自己最终的可执行文件变的很大,
有2种方式:
(1)你的模板类不会生成很多模板实例,这样写成模板类还有意义吗?
(2)你的模板类的代码量或是函数个数很少,你可以仿照ATL的方式把模板无关的东西用继承的方式逐层剥离,确保模板类的代码都是和模板参数相关的。