473,569 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function Template Question

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

-------------------------------------------------
template<typena me 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[i] < min_value)
min_value = r_array[i];
}

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\Te mplates\Templat es.cpp(91) : error C2265: '<Unknown>' :
reference to a zero-sized array is illegal
E:\TEMPLATES\Te mplates\Templat es.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
Jul 22 '05 #1
3 1618
* Dennis Pais:

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

-------------------------------------------------
template<typena me T, int size>
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[i] < min_value)
min_value = r_array[i];
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.
}
-------------------------------------------------

I am working with a Microsoft compiler (VC++ 6.0), & the above code
fails to compile


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
On 28 Jul 2004 05:07:25 -0700, pa*********@myr ealbox.com (Dennis Pais)
wrote:
Here's a piece of code that I found in C++ Primer by Stan Lippman !

-------------------------------------------------
template<typen ame 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[i] < min_value)
min_value = r_array[i];
}

return min_value;
A simpler implementation would be:
return *std::min_eleme nt(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\T emplates\Templa tes.cpp(91) : error C2265: '<Unknown>' :
reference to a zero-sized array is illegal
E:\TEMPLATES\T emplates\Templa tes.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
Jul 22 '05 #3
* Alf P. Steinbach:
* Dennis Pais:

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

-------------------------------------------------
template<typena me T, int size>


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[i] < min_value)
min_value = r_array[i];


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.
}
-------------------------------------------------

I am working with a Microsoft compiler (VC++ 6.0), & the above code
fails to compile


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.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2069
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ============================================================== Question 1: ============================================================== Compile the following codes, and run it.
0
1686
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > > ============================================================== > Your code refuses to compile. In function 'foo<a>' 't' is a pointer to > const. Friendship has nothing to do with it. When I...
2
1594
by: baumann | last post by:
hi all, i am reading the C++ templates complete guide, i have question on the following statement. We must therefore make sure the template parameters of the class template appear in the type of any friend function defined in that template (unless we want to prevent more than one instantiation of a class template in a particular file,...
16
16223
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
22
5973
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be given extern "C" linkage where it is to be assigned to a C function pointer. Ian
1
1396
by: sebastian | last post by:
Hi, I'd like to specialize a template function that contains a template parameter. In Example i have the following function declared: .... template < int i > static stupid_object& doSomething( int i, const another_stupid_object& __aso); ....
2
2305
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just want to know how it works with the overloading of get<>(). boost::tupple<int,doubletup(1,2.0); double d=tup.get<2>(); // equal 2.0 // simple...
9
2741
by: Mike | last post by:
Hi, Just a simple question: why the compiler doesn't report error when accessing a private member function inside a function having template type ? For example: #include<iostream> using namespace std;
5
2256
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are...
21
4673
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.