| Thread overview | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 26, 2012 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
void test1(T)(auto ref T) { }
void test2() { int a = 5; test1!(int)(a); }
Is there any reason this should fail?
| ||||
June 26, 2012 Re: 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On 06/26/2012 06:17 PM, Mehrdad wrote:
> void test1(T)(auto ref T) { }
> void test2() { int a = 5; test1!(int)(a); }
>
> Is there any reason this should fail?
There is not. I suppose the compiler completes instantiation without
looking at the function parameter.
| |||
June 27, 2012 Re: 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Tuesday, 26 June 2012 at 16:37:30 UTC, Timon Gehr wrote:
> On 06/26/2012 06:17 PM, Mehrdad wrote:
>> void test1(T)(auto ref T) { }
>> void test2() { int a = 5; test1!(int)(a); }
>>
>> Is there any reason this should fail?
>
> There is not. I suppose the compiler completes instantiation without
> looking at the function parameter.
Well the problem manifests itself in different ways, but would it be safe to say that
void foo(auto ref int) { }
should not cause problems either?
| |||
June 27, 2012 Re: 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On Wednesday, June 27, 2012 02:54:49 Mehrdad wrote:
> On Tuesday, 26 June 2012 at 16:37:30 UTC, Timon Gehr wrote:
> > On 06/26/2012 06:17 PM, Mehrdad wrote:
> >> void test1(T)(auto ref T) { }
> >> void test2() { int a = 5; test1!(int)(a); }
> >>
> >> Is there any reason this should fail?
> >
> > There is not. I suppose the compiler completes instantiation
> > without
> > looking at the function parameter.
>
> Well the problem manifests itself in different ways, but would it be safe to say that
>
> void foo(auto ref int) { }
>
> should not cause problems either?
That isn't legal. auto ref can only be used with templated functions.
- Jonathan M Davis
| |||
June 27, 2012 Re: 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Wednesday, 27 June 2012 at 03:54:12 UTC, Jonathan M Davis wrote:
> That isn't legal. auto ref can only be used with templated functions.
>
> - Jonathan M Davis
The trouble is, distinguishing between when it should be illegal seems like a blur to me.
Consider all the cases like:
- <Regular method>
- <Mixin method>
- <Template method>
- Lambdas (in all forms)
- Template class, <regular method>
- Template class, <template method>
- Regular class, <template method>
- Regular class, template method, <mixin method>
- Regular class, regular method, <mixin method>
- Template class, regular method, <mixin method>
- <Templates inside mixins>
- yada yada
It becomes a *nightmare* to try to get everything right, when you're dealing with templates and mixins. And you run into bugs like this.
Why not just allow 'auto ref' for every function type?
| |||
June 27, 2012 Re: 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On Wednesday, June 27, 2012 06:03:12 Mehrdad wrote:
> On Wednesday, 27 June 2012 at 03:54:12 UTC, Jonathan M Davis
>
> wrote:
> > That isn't legal. auto ref can only be used with templated functions.
> >
> > - Jonathan M Davis
>
> The trouble is, distinguishing between when it should be illegal seems like a blur to me.
>
> Consider all the cases like:
>
> - <Regular method>
> - <Mixin method>
> - <Template method>
> - Lambdas (in all forms)
> - Template class, <regular method>
> - Template class, <template method>
> - Regular class, <template method>
> - Regular class, template method, <mixin method>
> - Regular class, regular method, <mixin method>
> - Template class, regular method, <mixin method>
> - <Templates inside mixins>
> - yada yada
>
>
>
> It becomes a *nightmare* to try to get everything right, when you're dealing with templates and mixins. And you run into bugs like this.
>
> Why not just allow 'auto ref' for every function type?
And how would that work? auto ref duplicates the function. If you pass it an lvalue, then it generates a ref version. If you pass it an rvalue, it generates a non-ref version. For it to work with a normal function, you would somehow have to make it work with only one version. The original idea was for it to work for all functions, but Walter said that it can't be done. So, it only works with templated functions.
So, auto ref is pretty much a failure IMHO, since it was supposed to solve the problem of a single function being able to take both lvalues and rvalues efficiently.
There has been some discussion of making it so that rvalues work with ref in order to fix that problem, but that hasn't been sorted out yet, since it has to be done in a way that avoids the problems that C++ has allowing const& to take lvalues.
- Jonathan M Davis
| |||
June 27, 2012 Re: 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Wednesday, 27 June 2012 at 04:26:08 UTC, Jonathan M Davis wrote:
> And how would that work? auto ref duplicates the function. If you pass it an lvalue, then it generates a ref version.
Two solutions:
- Turn 'auto ref' into 'ref', but simply have the compiler generate a copy for the caller if he wants to pass by value?
- Treat it as though it were a templated parameter. Why does it make any difference here?
| |||
June 27, 2012 Re: 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On Wednesday, June 27, 2012 07:02:28 Mehrdad wrote: > On Wednesday, 27 June 2012 at 04:26:08 UTC, Jonathan M Davis > > wrote: > > And how would that work? auto ref duplicates the function. If you pass it an lvalue, then it generates a ref version. > > Two solutions: > > - Turn 'auto ref' into 'ref', but simply have the compiler generate a copy for the caller if he wants to pass by value? That at least sounds like it would work. Walter may have a reason why it doesn't though, since he's the one that said that he didn't think that it was possible. Maybe he just didn't think of it, or maybe it causes some other problem that I can't think of. > - Treat it as though it were a templated parameter. Why does it make any difference here? You can't just templatize functions. There are places where templated functions _can't_ be used. For instance, templated functions can't be virtual. And a templated function _can_ be used, then the programmer can just templatize it themselves, avoiding any possible issues caused by functions be automatically templatized. - Jonathan M Davis | |||
June 27, 2012 Re: 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
2012/6/27 Jonathan M Davis <jmdavisProg@gmx.com>: > On Wednesday, June 27, 2012 07:02:28 Mehrdad wrote: >> On Wednesday, 27 June 2012 at 04:26:08 UTC, Jonathan M Davis >> >> wrote: >> > And how would that work? auto ref duplicates the function. If you pass it an lvalue, then it generates a ref version. >> >> Two solutions: >> >> - Turn 'auto ref' into 'ref', but simply have the compiler generate a copy for the caller if he wants to pass by value? > > That at least sounds like it would work. Walter may have a reason why it doesn't though, since he's the one that said that he didn't think that it was possible. Maybe he just didn't think of it, or maybe it causes some other problem that I can't think of. After considering about 'auto ref', I was concluded that is an inconsistency of current language spec and we cannot fix it correctly. http://d.puremagic.com/issues/show_bug.cgi?id=8204 Therefore, I have created a thread to suggest new 'auto ref' recently. http://forum.dlang.org/thread/CAFDvkcvf6G8Mc01Tds6ydXqCZbfp1q-a-oeFVk6BGEtwCiUAqg@mail.gmail.com Kenji Hara >> - Treat it as though it were a templated parameter. Why does it make any difference here? > > You can't just templatize functions. There are places where templated functions _can't_ be used. For instance, templated functions can't be virtual. And a templated function _can_ be used, then the programmer can just templatize it themselves, avoiding any possible issues caused by functions be automatically templatized. > > - Jonathan M Davis | ||||
June 27, 2012 Re: 'Auto can only be used for template function arguments' what? | ||||
|---|---|---|---|---|
| ||||
On Wednesday, June 27, 2012 14:27:08 kenji hara wrote:
> 2012/6/27 Jonathan M Davis <jmdavisProg@gmx.com>:
> > On Wednesday, June 27, 2012 07:02:28 Mehrdad wrote:
> >> On Wednesday, 27 June 2012 at 04:26:08 UTC, Jonathan M Davis
> >>
> >> wrote:
> >> > And how would that work? auto ref duplicates the function. If you pass it an lvalue, then it generates a ref version.
> >>
> >> Two solutions:
> >>
> >> - Turn 'auto ref' into 'ref', but simply have the compiler generate a copy for the caller if he wants to pass by value?
> >
> > That at least sounds like it would work. Walter may have a reason why it doesn't though, since he's the one that said that he didn't think that it was possible. Maybe he just didn't think of it, or maybe it causes some other problem that I can't think of.
>
> After considering about 'auto ref', I was concluded that is an inconsistency of current language spec and we cannot fix it correctly.
>
> http://d.puremagic.com/issues/show_bug.cgi?id=8204
>
> Therefore, I have created a thread to suggest new 'auto ref' recently. http://forum.dlang.org/thread/CAFDvkcvf6G8Mc01Tds6ydXqCZbfp1q-a-oeFVk6BGEtwC iUAqg@mail.gmail.com
If it can be done, I'm all for it. We really need a solution to this problem, and conceptually, auto ref was a good idea, it just hasn't worked thus far.
- Jonathan M Davis
| ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply