www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const ref and rvalues

reply Trass3r <un known.com> writes:
I posted this question several times already and no answer yet,
why is allowing temporaries bind to ref const params bad again?
Feb 15 2012
next sibling parent Trass3r <un known.com> writes:
 why is allowing temporaries bind to ref const params bad again?
bernardh commented that they might not have an address, e.g. cause of being in a register.
Feb 15 2012
prev sibling next sibling parent reply "Alf P. Steinbach" <alf.p.steinbach+usenet gmail.com> writes:
On 16.02.2012 00:46, Trass3r wrote:
 I posted this question several times already and no answer yet,
 why is allowing temporaries bind to ref const params bad again?
It's supported in C++, isn't it supported by D? Cheers, - Alf (utter newbie)
Feb 15 2012
parent Trass3r <un known.com> writes:
 It's supported in C++, isn't it supported by D?
Andrei once said it was already a bad idea in C++.
Feb 16 2012
prev sibling next sibling parent Torarin <torarind gmail.com> writes:
2012/2/16 Trass3r <un known.com>:
 I posted this question several times already and no answer yet,
 why is allowing temporaries bind to ref const params bad again?
To give you the option, I think. const ref doesn't bind to r-values, while const auto ref does. Torarin
Feb 15 2012
prev sibling parent reply "Martin Nowak" <dawg dawgfoto.de> writes:
On Thu, 16 Feb 2012 00:46:35 +0100, Trass3r <un known.com> wrote:

 I posted this question several times already and no answer yet,
 why is allowing temporaries bind to ref const params bad again?
You'd want to pass temporaries by value, because then they can be constructed saving you a copy. If you want a function that avoids copies for lvalues and rvalues then you need auto ref :(. C++'s 'const &' is very easy to get wrong.
Feb 15 2012
parent reply Don Clugston <dac nospam.com> writes:
On 16/02/12 06:38, Martin Nowak wrote:
 On Thu, 16 Feb 2012 00:46:35 +0100, Trass3r <un known.com> wrote:

 I posted this question several times already and no answer yet,
 why is allowing temporaries bind to ref const params bad again?
You'd want to pass temporaries by value, because then they can be constructed saving you a copy. If you want a function that avoids copies for lvalues and rvalues then you need auto ref :(.
Yes but that doesn't work for const.
 C++'s 'const &' is very easy to get wrong.
Yes, but D still hasn't got it right. As far as I can tell, "const ref" in a parameter list is ALWAYS a bug.
Feb 16 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 02/16/2012 11:07 AM, Don Clugston wrote:
 On 16/02/12 06:38, Martin Nowak wrote:
 On Thu, 16 Feb 2012 00:46:35 +0100, Trass3r <un known.com> wrote:

 I posted this question several times already and no answer yet,
 why is allowing temporaries bind to ref const params bad again?
You'd want to pass temporaries by value, because then they can be constructed saving you a copy. If you want a function that avoids copies for lvalues and rvalues then you need auto ref :(.
Yes but that doesn't work for const.
I think it does. What would be the problem?
 C++'s 'const &' is very easy to get wrong.
Yes, but D still hasn't got it right. As far as I can tell, "const ref" in a parameter list is ALWAYS a bug.
Why? It is perfectly reasonable to pass a large struct by const ref.
Feb 16 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, February 16, 2012 16:12:51 Timon Gehr wrote:
 On 02/16/2012 11:07 AM, Don Clugston wrote:
 On 16/02/12 06:38, Martin Nowak wrote:
 C++'s 'const &' is very easy to get wrong.
Yes, but D still hasn't got it right. As far as I can tell, "const ref" in a parameter list is ALWAYS a bug.
Why? It is perfectly reasonable to pass a large struct by const ref.
Because then you _have_ to have a variable to call the function - except for the bizarre situation that struct literals have where they're considered lvalues (very bad idea IMHO). I think that from the perspective of most programmers, the fact that const ref doesn't take rvalues is a major negative, even if Andrei is ultimately right (though I don't even remember what his reasoning is - I'm not sure that I've ever understod it). The current solution is to use auto ref, but that only works with templates (I don't know if that's the plan or just a bug of the current implementation). So, the current situation is definitely broken. If auto ref is changed to work with non-templated functions, then _why_ would you ever use const ref? You end up with a function that will only work with lvalues but where you don't actually intend to change the original. So, you probably end up with functions being either auto ref (or const auto ref) or ref with no const ref, making const ref ultimately rather pointless. - Jonathan M Davis
Feb 16 2012
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 02/16/2012 05:36 PM, Jonathan M Davis wrote:
 On Thursday, February 16, 2012 16:12:51 Timon Gehr wrote:
 On 02/16/2012 11:07 AM, Don Clugston wrote:
 On 16/02/12 06:38, Martin Nowak wrote:
 C++'s 'const&' is very easy to get wrong.
Yes, but D still hasn't got it right. As far as I can tell, "const ref" in a parameter list is ALWAYS a bug.
Why? It is perfectly reasonable to pass a large struct by const ref.
Because then you _have_ to have a variable to call the function - except for the bizarre situation that struct literals have where they're considered lvalues (very bad idea IMHO). I think that from the perspective of most programmers, the fact that const ref doesn't take rvalues is a major negative, even if Andrei is ultimately right (though I don't even remember what his reasoning is - I'm not sure that I've ever understod it). The current solution is to use auto ref, but that only works with templates (I don't know if that's the plan or just a bug of the current implementation). So, the current situation is definitely broken. If auto ref is changed to work with non-templated functions, then _why_ would you ever use const ref? You end up with a function that will only work with lvalues but where you don't actually intend to change the original. So, you probably end up with functions being either auto ref (or const auto ref) or ref with no const ref, making const ref ultimately rather pointless. - Jonathan M Davis
Ok. Is there a ticket for implementing auto ref for non-templated functions already?
Feb 16 2012
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, February 16, 2012 18:40:39 Timon Gehr wrote:
 Ok. Is there a ticket for implementing auto ref for non-templated
 functions already?
I don't see one, but I'm not even 100% certain that it's supposed to. I just know that it doesn't now (making it of limited usefulness) and that Andrei was complaining that Walter had misunderstood how auto ref was supposed to work and implemented it wrong. I _think_ that the issue was that Walter had implemented it as a template thing, and it was supposed to work in general, but I'm not sure. A bug report should probably be opened for it though. If it's not supposed to work, then Walter can close it. - Jonathan M Davis
Feb 16 2012
parent reply Trass3r <un known.com> writes:
 I _think_ that the issue was that Walter had
 implemented it as a template thing, and it was supposed to work in  
 general, but I'm not sure.
How could it ever work without being a template?
Feb 16 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/17/2012 01:44 AM, Trass3r wrote:
 I _think_ that the issue was that Walter had
 implemented it as a template thing, and it was supposed to work in
 general, but I'm not sure.
How could it ever work without being a template?
The caller would provide the storage.
Feb 16 2012
prev sibling parent Trass3r <un known.com> writes:
 Because then you _have_ to have a variable to call the function - except  
 for the bizarre situation that struct literals have where they're  
 considered
 lvalues (very bad idea IMHO). I think that from the perspective of most
 programmers, the fact that const ref doesn't take rvalues is a major  
 negative,
 even if Andrei is ultimately right (though I don't even remember what his
 reasoning is - I'm not sure that I've ever understod it).
I guess it's what I said earlier, temporaries don't necessarily have an address, e.g. being in a register.
Feb 16 2012