等效于 C++ 中 Rust 的特定模板用法 [英] Equivalent of specific template usage in C++ for Rust

查看:43
本文介绍了等效于 C++ 中 Rust 的特定模板用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rust 中是否有一个功能可以让这样的事情成为可能?据我所知,这在 Rust 的泛型函​​数中是不可能的,因为它们只适用于数据类型,而不适用于值.

Is there a feature in Rust that makes something like this possible? As far as I know, this is not possible with Rust's generic functions because they work only with data types and not with values.

#include <iostream>

template<int T>
int foo(int a)
{
  return -1;
}
template<>
int foo<2>(int a)
{
  return a*a;
}
template<>
int foo<3>(int a)
{
  return a*a*a;
}

int main()
{
  std::cout << "foo<1>(3): "<<foo<1>(3) << std::endl;
  std::cout << "foo<2>(3): "<<foo<2>(3) << std::endl;
  std::cout << "foo<3>(3): "<<foo<3>(3) << std::endl;
  return 1;
}

结果:

foo<1>(3): -1
foo<2>(3): 9
foo<3>(3): 27

推荐答案

TL;DR:还没有,也许永远都没有.

Rust 泛型还没有 C++ 模板那么强大,而且可能永远不会如此.

Rust generics are not as powerful, yet, as C++ templates, and may never be.

特别是这里,需要两个功能:

Specifically here, two features are required:

  • RFC 2000: Const generics: which will enable non-type generic parameters,
  • RFC 2000: Specialization on const generics.

注意:目前还不清楚专业化的先进程度;在这种使用完全专业化的特定情况下,它应该足够了,但是尚不清楚是否会实现部分专业化以及如何实现.

还有其他缺失的部分,尽管与本案无关:

There are also other missing pieces, though not related to this case:

  • RFC 1598:通用关联类型:相当于嵌套的模板<...>使用 ... = ...; 并允许模拟模板模板参数,
  • 可变参数:已经有多个 RFC,但似乎没有一个获得太多立足点.
  • RFC 1598: Generic Associated Types: equivalent to nested template <...> using ... = ...; and allowing to emulate template template parameters,
  • variadics: there have been multiple RFCs, but none seems to have gain much foothold.

嘲笑 Rust 开发人员很容易,或者因为缺乏成熟而对其不屑一顾;它也是不正确的.

It would be easy to deride Rust developers, or shrug it off as a lack of maturity; it would also be incorrect.

正如我所提到的,目前还不清楚 Rust 是否会获得其中的一些功能,不是因为开发人员无法实现它们,他们当然可以,他们当然可以已经,而是因为有非常注重把事情做对1.

As I mentioned, it is not clear that Rust will ever gain some of those features, not because the developers could not implement them, they certainly could, they certainly could have already, but because there is a strong focus in doing things right1.

例如,专业化是 C++ 中的噩梦.使用一组参数 A 实例化模板是 未定义行为,然后(或在另一个翻译单元中)以匹配 A 的方式对其进行特化.对于函数,这通常表现为链接器选择通用或专用版本......随机.调试起来不好玩.

For example, specialization is a nightmare in C++. It is Undefined Behavior to instantiate a template with a set of arguments A, and later (or in another translation unit) specialize it in a way that would match A. For functions, this generally manifests as the linker picking either the generic or the specialized version... randomly. It's not fun to debug.

对泛型的任何修改都会对类型系统的其余部分产生巨大的潜在影响,与其他语言功能的复杂交互,以及关于类型良好的程序含义的重大变化:

Any modifications to generics has large potential repercussions on the rest of the type system, complex interactions with other language features, and significant changes about what a well-typed program means:

  • 因此,他们受到严格审查,
  • 并且大力推动缓慢增量构建,以便一次评估这些影响、相互作用和变化.
  • they are therefore heavily scrutinized,
  • and there is a strong push toward going slow and building incrementally, so as to evaluate those impacts, interactions and changes one at a time.

简而言之,Rust 开发人员正在尝试构建一个原理良好的泛型系统,这并不容易.

In short, Rust developers are attempting to build a well-principled generics system, and it's not easy.

1 还存在对不必要的复杂性的担忧,因此添加功能并不是仅仅因为",而是需要激励用例,这些用例应该足够引人注目,以证明语言中的额外复杂性是合理的和编译器;但这完全是另一扇门.

这篇关于等效于 C++ 中 Rust 的特定模板用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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