digitalmars.D - const ref and rvalues
- Trass3r (2/2) Feb 15 2012 I posted this question several times already and no answer yet,
- Trass3r (2/3) Feb 15 2012 bernardh commented that they might not have an address, e.g. cause of
- Alf P. Steinbach (4/6) Feb 15 2012 It's supported in C++, isn't it supported by D?
- Trass3r (1/2) Feb 16 2012 Andrei once said it was already a bad idea in C++.
- Torarin (4/6) Feb 15 2012 To give you the option, I think. const ref doesn't bind to r-values,
- Martin Nowak (6/8) Feb 15 2012 You'd want to pass temporaries by value, because then they can be
- Don Clugston (4/12) Feb 16 2012 Yes, but D still hasn't got it right.
- Timon Gehr (3/17) Feb 16 2012 Why? It is perfectly reasonable to pass a large struct by const ref.
- Jonathan M Davis (16/24) Feb 16 2012 Because then you _have_ to have a variable to call the function - except...
- Timon Gehr (3/27) Feb 16 2012 Ok. Is there a ticket for implementing auto ref for non-templated
- Jonathan M Davis (9/11) Feb 16 2012 I don't see one, but I'm not even 100% certain that it's supposed to. I ...
- Trass3r (1/4) Feb 16 2012 How could it ever work without being a template?
- Timon Gehr (2/6) Feb 16 2012 The caller would provide the storage.
- Trass3r (2/10) Feb 16 2012 I guess it's what I said earlier, temporaries don't necessarily have an ...
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
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
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
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
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
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
On 16/02/12 06:38, Martin Nowak wrote:On Thu, 16 Feb 2012 00:46:35 +0100, Trass3r <un known.com> wrote:Yes but that doesn't work for const.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.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
On 02/16/2012 11:07 AM, Don Clugston wrote:On 16/02/12 06:38, Martin Nowak wrote:I think it does. What would be the problem?On Thu, 16 Feb 2012 00:46:35 +0100, Trass3r <un known.com> wrote:Yes but that doesn't work for const.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 :(.Why? It is perfectly reasonable to pass a large struct by const ref.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
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: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 DavisWhy? It is perfectly reasonable to pass a large struct by const ref.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
On 02/16/2012 05:36 PM, Jonathan M Davis wrote:On Thursday, February 16, 2012 16:12:51 Timon Gehr wrote:Ok. Is there a ticket for implementing auto ref for non-templated functions already?On 02/16/2012 11:07 AM, Don Clugston wrote:On 16/02/12 06:38, Martin Nowak wrote: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 DavisWhy? It is perfectly reasonable to pass a large struct by const ref.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
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
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
On 02/17/2012 01:44 AM, Trass3r wrote:The caller would provide the storage.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
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