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[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\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 3 1613
* Dennis Pais: Here's a piece of code that I found in C++ Primer by Stan Lippman !
------------------------------------------------- template<typename 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?
On 28 Jul 2004 05:07:25 -0700, pa*********@myrealbox.com (Dennis Pais)
wrote: 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[i] < min_value) min_value = r_array[i]; }
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
* Alf P. Steinbach: * Dennis Pais: Here's a piece of code that I found in C++ Primer by Stan Lippman !
------------------------------------------------- template<typename 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
...
|
by: Yueh-Wei Hu |
last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news:
==============================================================
> > Question 1:
> >...
|
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...
|
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 ]
|
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...
| |
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&...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |