65.9K
CodeProject is changing. Read more.
Home

Passing a const character * as a template argument

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (2 votes)

Jul 3, 2011

CPOL
viewsIcon

5434

Since you must pass the string at each instanciation points, it is useless to make a template argument for it.templatestruct CVHDialogTmpl{ CVHDialogTmpl(const char *ptr_) : ptr(ptr_) { } INT_PTR DoModal() { return t.DoModal(ptr); } Ty t; ...

Since you must pass the string at each instanciation points, it is useless to make a template argument for it.
template<class>
struct CVHDialogTmpl
{     
	CVHDialogTmpl(const char *ptr_) : ptr(ptr_)
	{
	}     
 
	INT_PTR DoModal()
	{
		return t.DoModal(ptr);
	}
	
	Ty t;
        const char *ptr;
}; 
We can reuse VHRes class as defined in the original text and uses the class like this.
CVHDialogTmpl<cxmldlgprjdlg> dlg(VHRes::name);
This will works with any string including those that are built at run-time. It will be less intensive of the compiler and the generated code will probably be more efficient (memory usage).