D 如何通过返回类型解决这个问题?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 D 中,编译器可以为您推断出返回类型。所以不需要
->; V
语法。或者如果您想更具体(但最好让编译器使用
auto
来确定类型!)像 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.or if you want to be more specific (but it's better to let the compiler figure out the type with
auto
!)Like C++, you cannot use
typeof(lhs + rhs)
in that place.我不是 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 */ }