Re: templates, arrays and size
"Nick Valeontis" <numlock@freemail.gr> wrote in message
news:f7b0u8$jc$1@mouse.otenet.gr...
:I am confused.
:
: Why does this not work?
:
: ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
----
: template<class T> void PrintArray(T data[], int start, int stop, int
: col_length = 3) {
: for (int i = start; i <= stop; i++)
: std::cout << std::setw(col_length) << data[i] << " ";
: std::cout << std::endl;
: }
:
: template<class T> void PrintArray(T data[], int col_length = 3) {
In this context, T data[] declares a parameter of
type pointer-to-T.
: //sizeof(data)/sizeof(typeof(data[0]) = 0 !:O
: PrintArray(data, 0, sizeof(data)/sizeof(typeof(data[0])),
col_length);
: }
What you want to do instead is declare the parameter
as a reference to an array:
template<class T, unsigned N> inline
void PrintArray(T (&data)[N], int col_length = 3)
{ PrintArray(data,0,N,col_length); }
This should do what you expected...
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com