473,385 Members | 1,655 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,385 software developers and data experts.

why reture "const &"?

template <class T>
inline T const& max (T const& a, T const& b)
{
// if a < b then use b else use a
return a<b?b:a;
}

thanks very much!!

Jul 22 '05 #1
12 2560
ze*******@gmail.com wrote:

why reture "const &"?

template <class T>
inline T const& max (T const& a, T const& b)
{
// if a < b then use b else use a
return a<b?b:a;
}


To prevent atrocities like

std::max(a, b) = 2;

and to allow passing and returning of rvalues.
Jonathan
Jul 22 '05 #2
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:Kc********************@weber.videotron.net...
ze*******@gmail.com wrote:
>
> why reture "const &"?
>
template <class T>
inline T const& max (T const& a, T const& b)
{
// if a < b then use b else use a
return a<b?b:a;
}


To prevent atrocities like

std::max(a, b) = 2;


Why is that an atrocity? You might want to assign the max of a and b to 2.

DW

Jul 22 '05 #3
zealotcat at gmail.com wrote:
template <class T>
inline T const& max (T const& a, T const& b)
{
// if a < b then use b else use a
return a<b?b:a;
}


If you return by value (T) instead of by const reference (T const&), you
get a carbon copy of the maximum, not the original. When returning by
(const) reference, the class T doesn't need to have a copy contructor.

BTW, the "const" is necessary because your parameters are const
references as well. You might consider having a non-const version as
well:

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

See also http://www.aristeia.com/Papers/C++Re...umns/jan95.pdf
Kind regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #4
zealotcat at gmail.com asked:
why return "const &"?
Jonathan Mcdougall replied: To prevent atrocities like

std::max(a, b) = 2;


What about calling some other non-const member function?

Suppose I'm programming a GUI, and I have an ordered group of buttons on
a window, so that I can say button A < button B. (E.g., based on tab
order.) I might want to do:

void MyFunc(Button & a, Button & b)
{
// Setting the "default property" (or whatever!)
// of the button that has the maximum value.
max(a, b).SetDefault(true);

}

Typically Button::SetDefault is a non-const member function. So I need
a max(a, b) that returns a non-const reference!
Kind regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #5

"David White" <no@email.provided> wrote in message
news:1_*****************@nasal.pacific.net.au...
To prevent atrocities like

std::max(a, b) = 2;


Why is that an atrocity? You might want to assign the max of a and b to 2.


Maybe so, but that code won't do it. max is a function that returns a value,
not a variable.
Jul 22 '05 #6
jeffc wrote:
"David White" <no@email.provided> wrote in message
news:1_*****************@nasal.pacific.net.au...
To prevent atrocities like

std::max(a, b) = 2;


Why is that an atrocity? You might want to assign the max of a and b to 2.

Maybe so, but that code won't do it. max is a function that returns a value,
not a variable.


std::max is a function that returns a const
reference, not a value nor a "variable".
Jonathan

Jul 22 '05 #7
Niels Dekker - no reply address wrote:
zealotcat at gmail.com asked:
why return "const &"?

Jonathan Mcdougall replied:
To prevent atrocities like

std::max(a, b) = 2;

What about calling some other non-const member function?


That is not what std::max was designed to do.
Suppose I'm programming a GUI, and I have an ordered group of buttons on
a window, so that I can say button A < button B. (E.g., based on tab
order.) I might want to do:

void MyFunc(Button & a, Button & b)
{
// Setting the "default property" (or whatever!)
// of the button that has the maximum value.
max(a, b).SetDefault(true);

}
That's quite an ugly function, imho. std::max
will do the correct thing if operator< is
implemented correctly, but getting the maximum or
minimum button makes no sense, whether they are
order by their text of tab stop order. That's a
misuse of std::max.
Typically Button::SetDefault is a non-const member function. So I need
a max(a, b) that returns a non-const reference!


Roll you own.
Jonathan
Jul 22 '05 #8
simont wrote:
Writing
| max(5,7) = 2;
doesn't "assign the max of 5 and 7", because you can't change that.
That wouldn't work anyway, neither with a max that returns (and takes)
references (no matter whether they're const or not), nor with one that
returns by value. So that wouldn't be a problem anyway.
What would it mean to do so?
An error.
The function 'max' is usually considered to be returning the larger of
two values, rather than performing side-effects or anything else.
However, even std::max doesn't do this. It returns a const reference, not a
value.
Unless you meant something like

| template <typename T>
| T& larger_of( T& a, T& b ) { return a>b ? a : b; }

where

| int a(5);
| int b(7);
| // change the larger integer for some reason ...
| larger_of(a,b) /= 2;
might make sense. That isn't what max means, though.


max means whatever its programmer makes it mean. I don't see a need to call
it larger_of instead of max.

Jul 22 '05 #9
"simont" <s_********@yahoo.co.uk> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
David White wrote:
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
std::max(a, b) = 2;


Why is that an atrocity? You might want to assign the max of a and b

to 2.

You /can't/ "assign the max of a and b to 2" in any meaningful sense.
Writing
| max(5,7) = 2;
doesn't "assign the max of 5 and 7", because you can't change that.
What would it mean to do so? That I want globally to switch integer
arithmetic to modulo 5?


Of course not. I wasn't referring to constants. I was referring to a and b,
and you could have an l-value version of the template that would work in
that case. I'm not suggesting that such a template should be provided. I'm
just disagreeing that "std::max(a, b) = 2;" is an atrocity. It looks quite
neat and elegant to me, even if hardly anyone would ever need it.

DW

Jul 22 '05 #10

simont wrote:
You /can't/ "assign the max of a and b to 2" in any meaningful sense.
Writing
| max(5,7) = 2;
doesn't "assign the max of 5 and 7", because you can't change that.


You can't assign 5 to 2 in any meaningful sense either:

5 = 2;

Does this mean we have to get rid of assignment operator??

Writing max(a, b) = c makes perfect sense either in math or in
programming
At least for those who knows what references are used for ;)
--
Vladimir

Jul 22 '05 #11
ze*******@gmail.com wrote:
template <class T>
inline T const& max (T const& a, T const& b)
{
// if a < b then use b else use a
return a<b?b:a;
}

thanks very much!!


Because the input variables are const references. You shouldn't return a
non-const reference to a const object.

By the way, why is everyone on this thread assuming that the original
poster is talking about std::max?

I do like the idea of max returning an lvalue. The expression

max(a, b) = 5;

is succint, clear, and unambiguous.

-dr
Jul 22 '05 #12
ze*******@gmail.com wrote:
template <class T>
inline T const& max (T const& a, T const& b)
{
// if a < b then use b else use a
return a<b?b:a;
}

thanks very much!!

The parameters when the function 'max' is called have type 'T const&' -
meaning 'a' and 'b' are T's ( whatever they are ) that cannot be altered
in this context ( the 'const' ), and reference semantics are used (
possibly to avoid pointer stuff, but there are other reasons ).
For the type 'T' one hopes that the '<' operator is satisfactorily and
sensibly defined with regard to the meaning and usage of type 'T'.
The expression 'a<b' is evaluated in terms of its equivalence to 0, so
the conditional-expression operator as applied ( 'a<b?b:a' ) will
represent 'a' if 'a<b' is non-zero ( 'true' }, and 'b' otherwise.
The function thus returns one of 'a' or 'b' according to that test, but
with the type 'T const&'.
Presumably, this is because if the 'max' function ought not alter 'a'
or 'b' ( the 'const' in the argument list suggests this intention ),
then neither should the function that called 'max' ( to which either 'a'
or 'b' will be returned ).
Assigning to max(a,b) would seem to violate that intent, that is:

max(a,b) = Whatever; // Whatever is of type 'T'.

would be illegal as it stands. Some other function named 'max', but with
a different signature, could be used however - a non-const version for
instance.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #13

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

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
6
by: hoox2 | last post by:
void push_front(Node const*& head, int data); Can someone tell me what it means for "const*&"? A reference or a pointer?
3
by: Alexander Farber | last post by:
Hi, does anyone have an idea, why do I get the following error (I have to use g++296 on RedHat Linux as compiler): In file included from r_dir.cpp:9: r_obey.h:262: declaration of `const...
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
6
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.