473,671 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2607
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******** ************@we ber.videotron.n et...
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(t rue);

}

Typically Button::SetDefa ult 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.provi ded> 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.provi ded> 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(t rue);

}
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::SetDefa ult 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_********@yah oo.co.uk> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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

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

Similar topics

2
16236
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 integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
6
4361
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
1859
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 AreaSet &CObeyFile::AreaSet () const' r_areaset.h:197: changes meaning of `AreaSet' from `class AreaSet'
4
14781
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 expression: .... snip ...
5
3432
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; &lt;me@mydomain.com&gt;</Address> </Addresses>
14
5924
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 because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
6
5581
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 normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less obtuse. I'm puzzled why this one programmer insisted on odd style everywhere. Maybe he's just...
0
8400
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8672
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6234
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5702
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.