Thread overview | |||||
---|---|---|---|---|---|
|
November 06, 2012 const ref template vs non-template | ||||
---|---|---|---|---|
| ||||
Why does g(cs) compile but not G(cs)? Thanks, Dan struct S{} void g(ref S i) {} void g(const ref S i) {} void G(T)(ref T i) {} void G(T)(const ref T i) {} void main() { S s; const(S) cs; g(s); g(cs); G(s); // Error: template p.G matches more than one template declaration, /p.d(5):G(T) and /p.d(6):G(T) // G(cs); } |
November 06, 2012 Re: const ref template vs non-template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | On 11/06/2012 09:18 AM, Dan wrote:
> Why does g(cs) compile but not G(cs)?
>
> Thanks,
> Dan
>
> struct S{}
>
> void g(ref S i) {}
> void g(const ref S i) {}
> void G(T)(ref T i) {}
> void G(T)(const ref T i) {}
>
> void main() {
> S s;
> const(S) cs;
> g(s);
> g(cs);
> G(s);
> // Error: template p.G matches more than one template declaration,
> /p.d(5):G(T) and /p.d(6):G(T)
> // G(cs);
> }
>
Because in the case of cs, T is 'const(S)', matching the following two instantiations:
1) ref const(S) i
2) const ref const(S) i
The second line has a redundant const. So, they end up having the same signature.
Ali
|
November 06, 2012 Re: const ref template vs non-template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 6 November 2012 at 18:34:04 UTC, Ali Çehreli wrote:
> Because in the case of cs, T is 'const(S)', matching the following two instantiations:
>
> 1) ref const(S) i
> 2) const ref const(S) i
>
> The second line has a redundant const. So, they end up having the same signature.
>
> Ali
Oh - that clears it up, thanks. G(T)(ref T i) is really potentially many functions whereas g(ref S i) is only one and no conflict. What is the standard approach in the face of templates to differentiate between const(T) and T?
If templates do you always do something like:
void G(T)(ref T i) if(isMutable!T) { }
void G(T)(const ref T i) { }
Thanks,
Dan
|
Copyright © 1999-2021 by the D Language Foundation