D 如何通过返回类型解决这个问题?

发布于 2024-12-13 19:36:46 字数 366 浏览 0 评论 0原文

C++11引入了新的函数声明语法,

auto func(T rhs, U lhs) -> V

这是为了解决旧C++标准中函数模板出现的一些问题。请阅读这篇简短的维基百科文章部分,了解有关该问题的详细信息:
> http://en.wikipedia.org/wiki/C%2B%2B11#Alternative_function_syntax

我的问题是,D也面临着同样的问题吗?如果是这样,它如何解决它(如果有的话)?

C++11 introduced a new syntax for function declaration,

auto func(T rhs, U lhs) -> V

This was to solve some problems that appeared in the old C++ standard with function templates. Read this short Wikipedia article section for details about the problem:
> http://en.wikipedia.org/wiki/C%2B%2B11#Alternative_function_syntax

My question is, does D confront with the same problem? If so, how does it fix it (if at all)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-12-20 19:36:46

在 D 中,编译器可以为您推断出返回类型。所以不需要 ->; V 语法。

auto func(T, U)(T lhs, U rhs) { return lhs + rhs; }

或者如果您想更具体(但最好让编译器使用 auto 来确定类型!)

typeof(T.init + U.init) func(T, U)(T lhs, U rhs) { return lhs + rhs; }

像 C++ 一样,您不能使用 typeof(lhs + rhs)在那个地方。

In D, the compiler can deduce the return type for you. So there's no need to have the -> V syntax.

auto func(T, U)(T lhs, U rhs) { return lhs + rhs; }

or if you want to be more specific (but it's better to let the compiler figure out the type with auto!)

typeof(T.init + U.init) func(T, U)(T lhs, U rhs) { return lhs + rhs; }

Like C++, you cannot use typeof(lhs + rhs) in that place.

你的他你的她 2024-12-20 19:36:46

我不是 100% 确定,但我相信您可以使用 typeof() 语法。所以应该可以这样做: typeof(rhs+lhs) func(T rhs, U lhs) { /* body */ }

I am not 100% sure, but I believe you can use the typeof(<expression>) syntax. So it should be possible to do something like: typeof(rhs+lhs) func(T rhs, U lhs) { /* body */ }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文