Thread overview | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 24, 2012 auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
This has probably been discussed before, so someone has probably already explained why this is a bad idea, but I can't remember why that would be, so I'm going to ask: Why can't we simply make auto ref work with non-templated functions by making it automatically generate both the ref and non-ref versions? So, if I do auto foo(auto ref S s) { /*do stuff*/ } I automatically get something like auto foo(S s) { foo(s); } auto foo(ref S s) { /*do stuff*/ } and auto foo(auto const ref S s) { /* do stuff* / } becomes auto foo(const S s) { foo(s); } auto foo(ref const S s) { /* do stuff */ } What problems does this cause? Why haven't we just done this already? And if that doesn't work, can we simply make it so that the compiler automatically creates a variable when you pass an rvalue to a non-templated auto ref function? So, with auto foo(auto ref S s) { /*do stuff*/ } you get auto foo(ref S s) { /* do stuff*/ } but when you call it with an rvalue like with S bar() { ... } foo(bar()); you get something like auto _rValue = bar(); foo(_rValue}; where _rValue leaves scope after the call to foo. Are there any problems that anyone can see with that? It just seems to me that it should be relatively easy to come up with a solution to make auto ref work with non-templated functions, and then we can solve the whole const ref issue and be done with it. - Jonathan M Davis |
December 24, 2012 Re: auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Monday, 24 December 2012 at 17:40:54 UTC, Jonathan M Davis wrote:
> This has probably been discussed before, so someone has probably already
> explained why this is a bad idea, but I can't remember why that would be, so
> I'm going to ask:
>
> Why can't we simply make auto ref work with non-templated functions by making
> it automatically generate both the ref and non-ref versions? So, if I do
>
> auto foo(auto ref S s) { /*do stuff*/ }
Is:
auto foo()(auto ref S e) { /* do stuff */ }
So hard to write?
(It's Christmas Eve, and I can't be bothered giving real arguments against right now - I suppose someone else will do this later... Merry Christmas!)
Robert
|
December 24, 2012 Re: auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert Clipsham | On Monday, December 24, 2012 20:43:06 Robert Clipsham wrote:
> Is:
>
> auto foo()(auto ref S e) { /* do stuff */ }
>
> So hard to write?
>
> (It's Christmas Eve, and I can't be bothered giving real arguments against right now - I suppose someone else will do this later... Merry Christmas!)
If nothing else, it doesn't work for classes, because templated functions can't be virtual. Also, it forces you to put the entire function's implementation in a .di file if you use .di files, which makes it untentable for many of the folks who actually need to use .di files.
We need a solution which doesn't involve templates.
- Jonathan M Davis
|
December 24, 2012 Re: auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | > If nothing else, it doesn't work for classes, because templated functions
> can't be virtual. Also, it forces you to put the entire function's
> implementation in a .di file if you use .di files, which makes it untentable for
> many of the folks who actually need to use .di files.
>
> We need a solution which doesn't involve templates.
>
> - Jonathan M Davis
+1
|
December 25, 2012 Re: auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Monday, 24 December 2012 at 17:40:54 UTC, Jonathan M Davis wrote: > Why can't we simply make auto ref work with non-templated functions by making > it automatically generate both the ref and non-ref versions? > > [snip] > > What problems does this cause? Why haven't we just done this already? What does this generate? auto foo(auto ref S a, auto ref S b, auto ref S c, auto ref S d) { ... } 16 different functions, one for each combination? Sounds like a bad idea. > And if that doesn't work, can we simply make it so that the compiler > automatically creates a variable when you pass an rvalue to a non-templated > auto ref function? I don't see any problems with this, but I admittedly haven't thought too much about it. |
December 25, 2012 Re: auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Alexander | On Tuesday, 25 December 2012 at 00:56:44 UTC, Peter Alexander wrote:
> On Monday, 24 December 2012 at 17:40:54 UTC, Jonathan M Davis wrote:
>> And if that doesn't work, can we simply make it so that the compiler
>> automatically creates a variable when you pass an rvalue to a non-templated
>> auto ref function?
>
> I don't see any problems with this, but I admittedly haven't thought too much about it.
If there are no problems with this way, then what I want to know is why the template version of auto ref wasn't implemented this way. The way auto ref is currently implemented for templates is a bit of a mess.
|
December 25, 2012 Re: auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Alexander | On Tuesday, 25 December 2012 at 01:40:16 UTC, Peter Alexander wrote:
> On Tuesday, 25 December 2012 at 00:56:44 UTC, Peter Alexander wrote:
>> On Monday, 24 December 2012 at 17:40:54 UTC, Jonathan M Davis wrote:
>>> And if that doesn't work, can we simply make it so that the compiler
>>> automatically creates a variable when you pass an rvalue to a non-templated
>>> auto ref function?
>>
>> I don't see any problems with this, but I admittedly haven't thought too much about it.
>
> If there are no problems with this way, then what I want to know is why the template version of auto ref wasn't implemented this way. The way auto ref is currently implemented for templates is a bit of a mess.
Maybe it's difficult to generate both versions because for the function like this
void foo(auto ref S s1,auto ref S s2,...,auto ref s10)
compiler should generate 2^10 versions of function foo.
|
December 25, 2012 Re: auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Alexander | On Tuesday, December 25, 2012 01:56:42 Peter Alexander wrote: > On Monday, 24 December 2012 at 17:40:54 UTC, Jonathan M Davis > > wrote: > > Why can't we simply make auto ref work with non-templated > > functions by making > > it automatically generate both the ref and non-ref versions? > > > > [snip] > > > > What problems does this cause? Why haven't we just done this already? > > What does this generate? > > auto foo(auto ref S a, auto ref S b, auto ref S c, auto ref S d) > { ... } > > 16 different functions, one for each combination? Sounds like a bad idea. Excellent point. > > And if that doesn't work, can we simply make it so that the > > compiler > > automatically creates a variable when you pass an rvalue to a > > non-templated > > auto ref function? > > I don't see any problems with this, but I admittedly haven't thought too much about it. I hope that it's feasible (or something close to it anyway) so that we can actually solve this problem once and for all. - Jonathan M Davis |
December 25, 2012 Re: auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Alexander | > What does this generate?
>
> auto foo(auto ref S a, auto ref S b, auto ref S c, auto ref S d) { ... }
>
> 16 different functions, one for each combination? Sounds like a bad idea.
In my opinion, this should produce only two functions:
#1: auto foo(ref S a, ref S b, ref S c, ref S d) { ... }
#2: auto foo(S a, S b, S c, S d) { ... }
|
December 25, 2012 Re: auto ref and non-templated functions | ||||
---|---|---|---|---|
| ||||
Jonathan M Davis wrote:
> On Tuesday, December 25, 2012 01:56:42 Peter Alexander wrote:
> > On Monday, 24 December 2012 at 17:40:54 UTC, Jonathan M Davis
> >
> > wrote:
> > > Why can't we simply make auto ref work with non-templated
> > > functions by making
> > > it automatically generate both the ref and non-ref versions?
> > >
> > > [snip]
> > >
> > > What problems does this cause? Why haven't we just done this already?
> >
> > What does this generate?
> >
> > auto foo(auto ref S a, auto ref S b, auto ref S c, auto ref S d)
> > { ... }
> >
> > 16 different functions, one for each combination? Sounds like a bad idea.
>
> Excellent point.
I'm not sure but doesn't the compiler only create such a function when
it is called.
Still it may be confusing but it's similar to a function template. But
here it's a template regarding passing convention.
Jens
|
Copyright © 1999-2021 by the D Language Foundation