D中的模板参数推断 [英] Template argument inference in D

查看:67
本文介绍了D中的模板参数推断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一些在静态D数组上运行的向量函数,如下所示:

I am writing some vector functions which operate on static D arrays like so:

real[N] unit(uint N)(real[N] v) {
    real[N] u = (v[] / norm(v)); //explicit type necessary to force slice-operator
    return u;                    //to return static-length array
}

real[3] v = unit!(3)([1,2,3]);   //works
real[3] w = unit([1,2,3]);       //error, compiler doesn't infer the template parameter
real[3] x = [1,2,3];
auto i = unit(x);                //also works, forces statically allocated array

所以,我的问题是,有没有办法如果我将文字数组直接传递给函数,编译器将推断模板参数N?我尝试使用 1.0L格式,希望该数组是从int或float的静态数组强制转换而来的,但是那也不起作用。
TL; DR我可以使上面的中间示例(w)工作吗?谢谢!

So, my question is, is there a way to get the compiler to infer the template parameter N if I pass a literal array directly to the function? I tried using "1.0L" format, in the hopes that the array was being cast from a static array of int or float, but that didn't work either. TL;DR Can I make the middle example (w) above work? Thanks!

编辑:只是为了澄清一下,我尝试使用专门的模板参数进行一些变体,但是我不确定我是否正确地做到了。在通话中,我还尝试了 new real [3]([1,2,3])来强制分配堆分配的静态数组(三个有角的独角兽?)

Just to clarify, I've tried a few variations with specialized template parameters, but I'm not sure I've done that correctly. I have also tried, in the call, new real[3]([1,2,3]) to force a heap allocated static array (a three horned unicorn?) but I couldn't get that to compile.

推荐答案

问题是 [1,2, 3] 不是静态数组。这是一个动态数组,因此不能匹配。这是错误的类型,无法使用静态数组文字。如果要将数组文字作为静态数组传递,则需要先将其分配给变量,或者将其强制转换为所需的类型。

The problem is that [1,2,3] is not a static array. It's a dynamic array, so it can't match. It's the wrong type, and there's no way to have a static array literal. If you want to pass an array literal as a static array, you're going to need to either assign it to a variable first or cast it to the type that you want.

auto w = unit(cast(real[3])[1,2,3]);

应该可以。就我个人而言,我认为最好是显式实例化模板

should work. Personally, I'd argue that it's best to just explicitly instantiate the template

auto w = unit!3([1, 2, 3]);

因为它避免了搞砸演员阵容的风险。

because it avoids the risk of screwing up the cast.

现在,我认为有一个明确的论点,认为编译器应该在这种情况下才可以工作,但是与模板相比,对模板来说,它的挑剔性更高,因为它通常会实例化您传递的确切类型的模板。它不需要尝试进行任何隐式转换,而普通函数会隐式地将动态数组转换为静态数组。随时打开增强功能请求。该行为可能会更改。它最近进行了更改,以便IFTI(隐式函数模板实例化)使用数组的尾部常量版本进行实例化(例如 immutable(char)[] 而不是 immutable(char [])),这是一个明显的改进。现在,这与尝试进行转换有点不同(我相信编译器会自动将数组始终视为IFTI的tail-const),所以我不知道在这种情况下更改编译器行为的几率非常高高。但这并不麻烦问。

Now, I think that there's a definite argument that the compiler should just work in this case, but it tends to be much pickier with templates than with normal functions, since it generally instantiates templates with the exact type that you pass it without trying to do any implicit conversions, whereas a normal function would implicitly convert the dynamic array to a static one. Feel free to open an enhancement request. The behavior might get changed. It was recently changed so that IFTI (implicit function template instantiation) instantiates with the tail-const version of an array (e.g. immutable(char)[] instead of immutable(char[])), which has been a definite improvement. Now, that's a bit different than attempting a conversion (I believe that the compiler just automatically, always treats arrays as tail-const for IFTI), so I don't know that the odds of getting the compiler's behavior changed in this case are very high. But it doesn't hurt to ask.

这篇关于D中的模板参数推断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆