472,988 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 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 2522
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.