473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Doubt in C++ Templates - Josuttis.

SK
Hi all,

I have a doubt in C++ Templates by Nicolai M. Josuttis.
On Page 17 there is a line "In general, it is a good idea not to
change more than necessary when overloading function templates. You
should limit your changes to the number of parameters or to specifying
template parameters explicitly."

Can anyone explain the meaning of these lines to me?

Also the example quoted is -

template<typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}

inline T const& max (char const* a, char const* b)
{
return stdcmp(a, b) < 0 ? b : a;
}

template<typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return max (max(a, b), c); // Error, if max(a, b) uses pass by value
}
The reason for error given is that because for C-strings, max(a, b)
creates a new, temp local value that MAY be returned by the function
by reference.
I have also not been able to understand the reason for error given by
him.
Thanks and regards,
SK
Jul 22 '05 #1
1 1685
"SK" <sk******@rediffmail.com> wrote...
I have a doubt in C++ Templates by Nicolai M. Josuttis.
Wow, a double in an entire book after reading mere 17 pages?
On Page 17 there is a line "In general, it is a good idea not to
change more than necessary when overloading function templates. You
should limit your changes to the number of parameters or to specifying
template parameters explicitly."

Can anyone explain the meaning of these lines to me?
If you change too much, you muddy the _reason_ for overloading.
Besides you may run into problems similar to what they describe
in the comment: the error is due to the fact that some functions'
parameter passing method is different.
Also the example quoted is -

template<typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}

inline T const& max (char const* a, char const* b)
You could at least give the author the respect of quoting right.
Actually, it's

inline char const* max(char const* a, char const* b)
{
return stdcmp(a, b) < 0 ? b : a;
}

template<typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return max (max(a, b), c); // Error, if max(a, b) uses pass by value
The comment should probably say

// Error if max(a,b) _returns_ a temporary by value
}
The reason for error given is that because for C-strings, max(a, b)
creates a new, temp local value that MAY be returned by the function
by reference.
I have also not been able to understand the reason for error given by
him.


In max(a,b,c) all arguments are _references_. Now, if 'max(a,b)'
returns by value (when a, b, and c are char const*), the returned
value is a temporary char const*. In order to call the next 'max'
a _reference_ to that temporary has to be created and _can_ be
returned from the function (since max(a,b,c) returns by reference).
That's an error.

Victor

Jul 22 '05 #2

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

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: Ray Gardener | last post by:
Would templates be necessary if C++ had all numeric types inherit from a base "number" class and had all types inherit from a base "object" class? (Sorry if this has already been discussed to...
1
by: Leslaw Bieniasz | last post by:
Cracow, 15.09.2004 Hi, I am writing a big C++ project using BCB 4.0. The project consists of several dlls and exes. Each of the dll is composed of several units (that is cpp files with...
14
by: Jigar Mehta | last post by:
Hye all, I am a VC++ programmer (have exp. of 2+ years).. But till now, I have never worked on templates.. I have developed whole products but never worked on templates or used them in my...
13
by: Winbatch | last post by:
Hi, If this should be directed to another group, please let me know... I've been working with templates for a few weeks and have been able to develop some nice code on solaris using the Forte C++...
16
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 ]
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
6
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
2
by: madhu.srikkanth | last post by:
Hi, I came across a paper by Angelika Langer in C++ Users Journal on Expression Templates. In the article she had mentioned that the code snippet below used to calculate a dot product is an...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
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...
0
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 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.