function template

K

Kyle

I found this in my old junk testing path...

/* get the size of the array */
template<typename T, int size>
int getsz (T a[size]) { return size;}

Seems the compiler (gcc 3.4) can not compile the code when the template
is used, it can not deduct the parameter.

Was this code supposed to work? If not, which template parameter
deduction rule does it violate?

thanks!
 
V

Victor Bazarov

Kyle said:
I found this in my old junk testing path...

/* get the size of the array */
template<typename T, int size>
int getsz (T a[size]) { return size;}

Seems the compiler (gcc 3.4) can not compile the code when the template
is used, it can not deduct the parameter.

"cannot deduce", not "deduct".
Was this code supposed to work? If not, which template parameter
deduction rule does it violate?

It is impossible to deduce that is the arrays size from a pointer. You
need a reference to an array:

template<typename T, int size>
int getsz(T (&a)[size]) { return size; }

Of course, this will only compile if the argument you're passing is in
fact an array, not a pointer. But if it's so, you already know the
size of it at the point where you call that function (all arrays have
static dimensions). So, it seems that while such function is interesting
as a template exercise, I can't find a good use for it.

V
 
R

Rolf Magnus

Kyle said:
I found this in my old junk testing path...

/* get the size of the array */
template<typename T, int size>
int getsz (T a[size]) { return size;}

Seems the compiler (gcc 3.4) can not compile the code when the template
is used, it can not deduct the parameter.

Was this code supposed to work?
If not, which template parameter
deduction rule does it violate?

I'm not sure, but I think it's due to the fact that in a parameter,
'T a[size]' is actually the syntax for a pointer to T. 'size' is ignored.
So I guess that - when called - the argument is converted to T*, and so the
compiler has no way to figure out the size template argument.
After all, you could also call it as:

char* p;
getsz(p);

What would 'size' now be?
 
N

Neelesh Bodas

Kyle said:
I found this in my old junk testing path...

/* get the size of the array */
template<typename T, int size>
int getsz (T a[size]) { return size;}

Seems the compiler (gcc 3.4) can not compile the code when the template
is used, it can not deduct the parameter.

Was this code supposed to work? If not, which template parameter
deduction rule does it violate?
when array is passed as a reference, the size is also passed.

int getsz (T (&a) [size]) { return size;}

this would work.

Hope this helps.
 
V

Victor Bazarov

Kyle said:
Guys, Thanks for the kind reply and sorry for my bad English.

Don't worry about your English. "Deduce" and "deduct" have the common
root, 'ducere', Latin for "lead" [out of]. The difference is subtle (if
any at all). In modern English "deduct" chiefly means "remove, subtract"
(as in "expenses from being taxed", for example), whereas "deduce" means
"to come to a logical conclusion by means of deduction", mainly. Both
involve elimination of some kind. Besides, "deduct" does have the second
meaning "infer, deduce". I only corrected you because the Standard uses
the term "deduce" for figuring out the template arguments.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top