Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 23, 2017 Parameterized template value parameter | ||||
---|---|---|---|---|
| ||||
So I was trying to make my template take a value parameter, whose type is also a parameter to the template. e.g.: template A(Char[] str, Char); But dmd complains about 'Char' being undefined. I have to write: template A(Char, Char[] str); Which is inconvenient, because now 'Char' can't be deduced by the compiler. Can we make the first case work? |
March 24, 2017 Re: Parameterized template value parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | On 3/24/17 12:24 AM, Yuxuan Shui wrote:
> So I was trying to make my template take a value parameter, whose type
> is also a parameter to the template. e.g.:
>
> template A(Char[] str, Char);
>
> But dmd complains about 'Char' being undefined. I have to write:
>
> template A(Char, Char[] str);
>
> Which is inconvenient, because now 'Char' can't be deduced by the compiler.
>
> Can we make the first case work?
How about this?
template A(alias str)
if(is(typeof(str) : Char[], Char)){
alias Char = typeof(str[0]);
// ...
}
In general it's sometimes possible to do the deduction w/o introducing more template arguments.
---
Dmitry Olshansky
|
March 24, 2017 Re: Parameterized template value parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | On Friday, 24 March 2017 at 20:43:18 UTC, Dmitry Olshansky wrote: > On 3/24/17 12:24 AM, Yuxuan Shui wrote: >> So I was trying to make my template take a value parameter, whose type >> is also a parameter to the template. e.g.: >> >> template A(Char[] str, Char); >> >> But dmd complains about 'Char' being undefined. I have to write: >> >> template A(Char, Char[] str); >> >> Which is inconvenient, because now 'Char' can't be deduced by the compiler. >> >> Can we make the first case work? > > How about this? > > template A(alias str) > if(is(typeof(str) : Char[], Char)){ > alias Char = typeof(str[0]); > // ... > } Yes. This is what I ended up doing (https://github.com/yshui/sdpc/blob/master/sdpc/parsers.d#L45). One problem of this is that 'str' is not longer restricted to be a compile time value. Which is probably fine, but kind of makes me uncomfortable. > > In general it's sometimes possible to do the deduction w/o introducing more template arguments. > > --- > Dmitry Olshansky |
March 25, 2017 Re: Parameterized template value parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | On Friday, 24 March 2017 at 21:13:26 UTC, Yuxuan Shui wrote: > On Friday, 24 March 2017 at 20:43:18 UTC, Dmitry Olshansky >> >> template A(alias str) >> if(is(typeof(str) : Char[], Char)){ >> alias Char = typeof(str[0]); >> // ... >> } > One problem of this is that 'str' is not longer restricted to be a compile time value. Which is probably fine, but kind of makes me uncomfortable. To test wether str is a compile time value just check if you can use it to assign to an enum if(is(typeof(str) : Char[], Char) && is(typeof({enum foo = str;}))) |
Copyright © 1999-2021 by the D Language Foundation