实现一个模板化结构的默认值,其中唯一的区别是模板化的变量。

huangapple go评论99阅读模式
英文:

implement Default for a templated struct where the only difference is the templated variable

问题

在定义了一个具有长度模板的矩阵之后,以便我可以更轻松地控制大小:

  1. pub struct SquareMatrix<const length: usize> {
  2. data: [[f32; length]; length],
  3. }

我为该矩阵定义了默认函数:

  1. impl Default for SquareMatrix<2> {
  2. fn default() -> Self {
  3. return SquareMatrix {
  4. data: [[0.0; 2]; 2],
  5. }
  6. }
  7. }
  8. impl Default for SquareMatrix<3> {
  9. fn default() -> Self {
  10. return SquareMatrix {
  11. data: [[0.0; 3]; 3],
  12. }
  13. }
  14. }
  15. impl Default for SquareMatrix<4> {
  16. fn default() -> Self {
  17. return SquareMatrix {
  18. data: [[0.0; 4]; 4],
  19. }
  20. }
  21. }

这看起来不够优雅,因为它们唯一的区别是模板的长度。但是,当我去掉length并且只定义一个有模板的函数时:

  1. impl Default for SquareMatrix<length> {
  2. fn default() -> Self {
  3. return SquareMatrix {
  4. data: [[0.0; length]; length],
  5. }
  6. }
  7. }

出现错误提示:length not found in this scope

是否有一种方法可以为模板结构定义一个模板函数?

英文:

After defining a matrix where the length templated so I can control the size easier:

  1. pub struct SquareMatrix&lt;const length: usize&gt; {
  2. data: [[f32; length]; length],
  3. }

I define default functions for the matrix:

  1. impl Default for SquareMatrix&lt;2&gt; {
  2. fn default() -&gt; Self {
  3. return SquareMatrix {
  4. data: [[0.0; 2];2],
  5. }
  6. }
  7. }
  8. impl Default for SquareMatrix&lt;3&gt; {
  9. fn default() -&gt; Self {
  10. return SquareMatrix {
  11. data: [[0.0; 3];3],
  12. }
  13. }
  14. }
  15. impl Default for SquareMatrix&lt;4&gt; {
  16. fn default() -&gt; Self {
  17. return SquareMatrix {
  18. data: [[0.0; 4];4],
  19. }
  20. }
  21. }

It does not look elegant as the only difference of them is the length which is templated. But when I take out the length and define only 1 templated function:

  1. impl Default for SquareMatrix&lt;length&gt; {
  2. fn default() -&gt; Self {
  3. return SquareMatrix {
  4. data: [[0.0; length]; length],
  5. }
  6. }
  7. }

Error prompt: length not found in this scope

Is there a way to define a templated function for templated struct?

答案1

得分: 3

你已经很接近了,你只需要在Default实现中定义泛型参数:

  1. #[derive(Debug)]
  2. pub struct SquareMatrix<const LENGTH: usize> {
  3. data: [[f32; LENGTH]; LENGTH],
  4. }
  5. impl<const LENGTH: usize> Default for SquareMatrix<LENGTH> {
  6. fn default() -> Self {
  7. return SquareMatrix {
  8. data: [[0.0; LENGTH]; LENGTH],
  9. };
  10. }
  11. }
  12. fn main() {
  13. let matrix: SquareMatrix<2> = Default::default();
  14. println!("{:?}", matrix);
  15. }
  1. SquareMatrix { data: [[0.0, 0.0], [0.0, 0.0]] }

还有一点小问题:const泛型应该使用全大写字母表示。

英文:

You are close, you just need to define the generic in the Default implementation:

  1. #[derive(Debug)]
  2. pub struct SquareMatrix&lt;const LENGTH: usize&gt; {
  3. data: [[f32; LENGTH]; LENGTH],
  4. }
  5. impl&lt;const LENGTH: usize&gt; Default for SquareMatrix&lt;LENGTH&gt; {
  6. fn default() -&gt; Self {
  7. return SquareMatrix {
  8. data: [[0.0; LENGTH]; LENGTH],
  9. };
  10. }
  11. }
  12. fn main() {
  13. let matrix: SquareMatrix&lt;2&gt; = Default::default();
  14. println!(&quot;{:?}&quot;, matrix);
  15. }
  1. SquareMatrix { data: [[0.0, 0.0], [0.0, 0.0]] }

Also, minor nitpick: const generics should be written in all caps.

huangapple
  • 本文由 发表于 2023年5月28日 11:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定