Function Template Question

D

Dennis Pais

Here's a piece of code that I found in C++ Primer by Stan Lippman !

-------------------------------------------------
template<typename T, int size>
T min(T (&r_array) [size] )
{
Type min_value = r_array[0];

for(int i=1; i<size; i++)
{
if( r_array < min_value)
min_value = r_array;
}

return min_value;
}

int ia[] = {1,2,3,4,5,6};

//to invoke the above function
int main()
{
int i = min(ia);
printf("Hello World");
}
-------------------------------------------------

I am working with a Microsoft compiler (VC++ 6.0), & the above code
fails to compile with the following error(s):
--------------------------------------------------------------
E:\TEMPLATES\Templates\Templates.cpp(91) : error C2265: '<Unknown>' :
reference to a zero-sized array is illegal
E:\TEMPLATES\Templates\Templates.cpp(124) : error C2784: 'T __cdecl
min(T (&)[1])' : could not deduce template argument for ' (&)[1]' from
'int [5]'
Error executing cl.exe.
---------------------------------------------------------------

Can someone tell me if the above code works with some other
non-Microsoft compiler & why the Microsoft compiler does not allow
this code to execute ?

-Thanks
Dennis
 
A

Alf P. Steinbach

* Dennis Pais:
Here's a piece of code that I found in C++ Primer by Stan Lippman !

Should be 'size_t', not 'int'.

T min(T (&r_array) [size] )

Efficiency: should return 'T const&' or type dependent on T.


Here should be assertion or check of size > 0.

Type min_value = r_array[0];

This should be

T const* min_value = &r_array[0];

and other use of 'min_value' adjusted accordingly.

for(int i=1; i<size; i++)

Style: preferentially use ++i, not i++.

{
if( r_array < min_value)
min_value = r_array;


Style: always use brackets for the 'if' body (and others).
}

return min_value;
}

This is less then ideal design. The 'min' function of array ref
argument should better forward to one with array + length or
begin+end pointers arguments. That would make it much more useful.

int ia[] = {1,2,3,4,5,6};

//to invoke the above function
int main()
{
int i = min(ia);
printf("Hello World");

Style: preferentially use type-safe C++ iostreams for small test
programs -- efficiency is not an issue, but clarity and safety is.

MSVC 6.0 is a very limited and not very standard-compliant.

Try a better compiler.

In effect, just about any other compiler, including MSVC 7.x.
 
T

tom_usenet

Here's a piece of code that I found in C++ Primer by Stan Lippman !

-------------------------------------------------
template<typename T, int size>
T min(T (&r_array) [size] )
{
Type min_value = r_array[0];

for(int i=1; i<size; i++)
{
if( r_array < min_value)
min_value = r_array;
}

return min_value;


A simpler implementation would be:
return *std::min_element(r_array, r_array + size);
}

int ia[] = {1,2,3,4,5,6};

//to invoke the above function
int main()
{
int i = min(ia);
printf("Hello World");
}
-------------------------------------------------

I am working with a Microsoft compiler (VC++ 6.0), & the above code
fails to compile with the following error(s):
--------------------------------------------------------------
E:\TEMPLATES\Templates\Templates.cpp(91) : error C2265: '<Unknown>' :
reference to a zero-sized array is illegal
E:\TEMPLATES\Templates\Templates.cpp(124) : error C2784: 'T __cdecl
min(T (&)[1])' : could not deduce template argument for ' (&)[1]' from
'int [5]'
Error executing cl.exe.
---------------------------------------------------------------

Can someone tell me if the above code works with some other
non-Microsoft compiler & why the Microsoft compiler does not allow
this code to execute ?

The code should work. MSVC has problems with templates in general. I
think the specific problem here is that it can't deduce array bounds
from arguments passed to template functions. There may be a
workaround, but I haven't got MSVC6 installed to experiment with.

Tom
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* Dennis Pais:
Here's a piece of code that I found in C++ Primer by Stan Lippman !

Should be 'size_t', not 'int'.

T min(T (&r_array) [size] )

Efficiency: should return 'T const&' or type dependent on T.

Forgot to mention: the argument should be 'T const (&r_array) [size]'.

Here should be assertion or check of size > 0.

Forgot to mention context: when the thing is redesigned as suggested
below.

Type min_value = r_array[0];

This should be

T const* min_value = &r_array[0];

and other use of 'min_value' adjusted accordingly.

for(int i=1; i<size; i++)

Style: preferentially use ++i, not i++.

{
if( r_array < min_value)
min_value = r_array;


Style: always use brackets for the 'if' body (and others).
}

return min_value;
}

This is less then ideal design. The 'min' function of array ref
argument should better forward to one with array + length or
begin+end pointers arguments. That would make it much more useful.

int ia[] = {1,2,3,4,5,6};

//to invoke the above function
int main()
{
int i = min(ia);
printf("Hello World");

Style: preferentially use type-safe C++ iostreams for small test
programs -- efficiency is not an issue, but clarity and safety is.

MSVC 6.0 is a very limited and not very standard-compliant.

Try a better compiler.

In effect, just about any other compiler, including MSVC 7.x.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top