Thread overview |
---|
September 14, 2007 template typeing issues | ||||
---|---|---|---|---|
| ||||
Is there a better way to get T[] and T[5] to play nice in a template? void main() { char[] c = "world"; t("hello", c); } void t(T, U)(T a, U b) { static assert(is(T : U) || is(U : T)); } my first attempt was this: void t(T)(T a, T b) { } But it wouldn't instance |
September 15, 2007 Re: template typeing issues | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS Attachments: | BCS wrote:
>
> Is there a better way to get T[] and T[5] to play nice in a template?
>
> void main()
> {
> char[] c = "world";
> t("hello", c);
> }
>
> void t(T, U)(T a, U b)
> {
> static assert(is(T : U) || is(U : T));
> }
>
> my first attempt was this:
>
> void t(T)(T a, T b) {
> }
>
> But it wouldn't instance
>
>
Here you go!
$ cat test8.d; echo -----; gdc test8.d -o test8 && ./test8
import std.stdio;
void main() {
char[] c="world";
t("hello", c);
}
void t(T)(T[] a, T[] b) {
writefln(a, " ", b);
}
- -----
hello world
--downs
|
September 15, 2007 Re: template typeing issues | ||||
---|---|---|---|---|
| ||||
Posted in reply to Downs | Reply to Downs,
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> BCS wrote:
>
>> Is there a better way to get T[] and T[5] to play nice in a template?
>>
>> void main()
>> {
>> char[] c = "world";
>> t("hello", c);
>> }
>> void t(T, U)(T a, U b)
>> {
>> static assert(is(T : U) || is(U : T));
>> }
>> my first attempt was this:
>>
>> void t(T)(T a, T b) {
>> }
>> But it wouldn't instance
>>
> Here you go!
>
> $ cat test8.d; echo -----; gdc test8.d -o test8 && ./test8
> import std.stdio;
> void main() {
> char[] c="world";
> t("hello", c);
> }
> void t(T)(T[] a, T[] b) {
> writefln(a, " ", b);
> }
> - -----
> hello world
t(4, 5); // fails for non arrays
The better way to describe what I need is: I need a template function that takes two args, whenever there is a type that both can implicitly convert to the template will implicitly instance for it.
|
Copyright © 1999-2021 by the D Language Foundation