By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Doubt in C++ Templates - Josuttis.

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.