Board index » cppbuilder » Template oddity

Template oddity


2008-04-11 06:16:04 PM
cppbuilder67
I'm trying to replace a macro-based "clear" function with a template
one - but without much luck.
template<typename T, std::size_t N>
void CLEAR(T(&item) [N])
{
memset(item, 0, sizeof(item[0]) * N);
};
void test1()
{
char message[128];
CLEAR(message); << This compiles fine
}
class test2
{
char message[128];
test2() {
CLEAR(message); << This doesn't
};
};
I get
[BCC32 Error] : E2285 Could not find a match for 'CLEAR<T,N>(char *)'
Have I got something wrong, or is this a BCB2007 bug?
- Roddy
 
 

Re:Template oddity

Roddy Pratt wrote:
Quote
Have I got something wrong, or is this a BCB2007 bug?
Report No: 50868 Status: Open
error occurs on passing non-static member to template function which
accepts reference to array
qc.codegear.com/wc/qcmain.aspx
QCWIN:Defect_No=50868
- Leo
 

Re:Template oddity

Leo Siefert wrote:
Quote
QCWIN:Defect_No=50868
ah. Thanks!
... just shows how out of touch I am with BCB QC reports at the
moment... oh well.
- Roddy
 

{smallsort}

Re:Template oddity

"Roddy Pratt" < XXXX@XXXXX.COM >writes:
Quote
template<typename T, std::size_t N>
void CLEAR(T(&item) [N])
{
memset(item, 0, sizeof(item[0]) * N);
};
Some side notes:
* this function name is very loud :-)
* why don't you simply use sizeof item (or, if you prefer, sizeof(item))
to determine the array size?
* std::fill_n(item,N,T()) or std::fill(item,item+N,T()) are more
generic and might allow the compiler to do better optimization
because the argument are guaranteed to be aligned at object
boundaries