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

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

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

问题

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

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

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

impl Default for SquareMatrix<2> {
    fn default() -> Self {
        return SquareMatrix {
            data: [[0.0; 2]; 2],
        }
    }
}

impl Default for SquareMatrix<3> {
    fn default() -> Self {
        return SquareMatrix {
            data: [[0.0; 3]; 3],
        }
    }
}

impl Default for SquareMatrix<4> {
    fn default() -> Self {
        return SquareMatrix {
            data: [[0.0; 4]; 4],
        }
    }
}

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

impl Default for SquareMatrix<length> {
    fn default() -> Self {
        return SquareMatrix {
            data: [[0.0; length]; length],
        }
    }
}

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

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

英文:

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

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

I define default functions for the matrix:

impl Default for SquareMatrix&lt;2&gt; {
    fn default() -&gt; Self {
        return SquareMatrix {
            data: [[0.0; 2];2],
        }
    }
}

impl Default for SquareMatrix&lt;3&gt; {
    fn default() -&gt; Self {
        return SquareMatrix {
            data: [[0.0; 3];3],
        }
    }
}

impl Default for SquareMatrix&lt;4&gt; {
    fn default() -&gt; Self {
        return SquareMatrix {
            data: [[0.0; 4];4],
        }
    }
}

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:

impl Default for SquareMatrix&lt;length&gt; {
        fn default() -&gt; Self {
        return SquareMatrix {
            data: [[0.0; length]; length],
        }
    }
}

Error prompt: length not found in this scope

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

答案1

得分: 3

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

#[derive(Debug)]
pub struct SquareMatrix<const LENGTH: usize> {
    data: [[f32; LENGTH]; LENGTH],
}

impl<const LENGTH: usize> Default for SquareMatrix<LENGTH> {
    fn default() -> Self {
        return SquareMatrix {
            data: [[0.0; LENGTH]; LENGTH],
        };
    }
}

fn main() {
    let matrix: SquareMatrix<2> = Default::default();

    println!("{:?}", matrix);
}
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:

#[derive(Debug)]
pub struct SquareMatrix&lt;const LENGTH: usize&gt; {
    data: [[f32; LENGTH]; LENGTH],
}

impl&lt;const LENGTH: usize&gt; Default for SquareMatrix&lt;LENGTH&gt; {
    fn default() -&gt; Self {
        return SquareMatrix {
            data: [[0.0; LENGTH]; LENGTH],
        };
    }
}

fn main() {
    let matrix: SquareMatrix&lt;2&gt; = Default::default();

    println!(&quot;{:?}&quot;, matrix);
}
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:

确定