cod*_*der 6 c++ generics templates rust
所以我只是在学习 rust - 我已经设置了工具并让它们在结构上工作,交叉编译,链接库等等......我有 C++ 背景)。所以在 C++ 谈话中我想这样做:
template<typename A, typename B>
auto add(A a, B b)
{
return a + b;
}
int main() {
std::cout << "add int int: " << add(1, 2) << std::endl;
std::cout << "add int double: " << add(1, 2.3f) << std::endl;
std::cout << "add int float: " << add(1, 2.3) << std::endl;
std::cout << "add char int: " << add('g', 2.3) << std::endl;
std::cout << "add str str: " << add(std::string("bob"), std::string("fred")) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
add int int: 3
add int double: 3.3
add int float: 3.3
add char int: 105.3
add str str: bobfred
Run Code Online (Sandbox Code Playgroud)
这增加了“事物”。如果您尝试添加两个未+使用正确操作数实现正确运算符的内容,您将收到编译错误(如添加字符串和整数) - 非常简单。
现在生锈(我完全没有掌握它。
普通添加功能
add int int: 3
add int double: 3.3
add int float: 3.3
add char int: 105.3
add str str: bobfred
Run Code Online (Sandbox Code Playgroud)
到目前为止还好。
尝试 1 将其模板化
我通过聆听错误并使用建议的更改(使用std::ops::Add<Output = T>我有点理解它,但我真的不知道为什么我必须指定这一点。
pub fn add(a: u64, b: u64) -> u64 {
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
这是意料之中的,因为我只告诉它一种类型......所以现在尝试两种类型 A,B - 但这是我迷路的地方:
pub fn add<T: std::ops::Add<Output = T>>(a: T, b: T) -> T
{
return a + b;
}
println!("add int int: {}", add(1, 2)); // ok
println!("add int float: {}", add(1, 2.3)); // fails to compile
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
error[E0308]: mismatched types
--> utils/src/lib.rs:19:16
|
17 | pub fn add<A: std::ops::Add<Output = A>, B: std::ops::Add>(a: A, b: B) -> A
| - expected type parameter - found type parameter
18 | {
19 | return a + b;
| ^ expected type parameter `A`, found type parameter `B`
|
= note: expected type parameter `A`
found type parameter `B`
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
Run Code Online (Sandbox Code Playgroud)
所以现在我完全迷失了。首先,我似乎必须指定一个返回类型 - 但我希望为我推断出它。我怎么做?其余的错误可能都是由于返回类型A- 但我似乎必须指定一些东西......这里的任何帮助都会很棒!- 如果可能的话,我想存档与上述 c++ 模板相同的基本功能
首先,您最好参考std::ops::Addfirst的文档。有一个type Output你已经设置的,但也有一个模板参数Rhs,你可以设置过。所以你可以试试:
pub fn add_to_rhs<A, B>(a: A, b: B) -> B
where A: std::ops::Add<B, Output = B>
{
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
在那里你声明 LHS 值应该与 RHS 值相加,最后产生一种 RHS 值。但这仍然无法按您预期的那样工作
println!("add int float: {}", add_to_rhs(1, 2.3));
// fails to compile, no implementation for `{integer} + {float}`
Run Code Online (Sandbox Code Playgroud)
这是合理的,因为弱类型不适合 Rust,因为它容易出现逻辑错误。什么是可能的解决方法?实际上类似于您在非通用程序中所做的事情——您将显式转换其中一个值:
pub fn add_to_rhs<A, B>(a: A, b: B) -> B
where A: Into<B>, B: std::ops::Add<Output = B>
{
return a.into() + b;
}
Run Code Online (Sandbox Code Playgroud)
这并没有达到与 C++ SFINAE 相同的灵活性(记住,没有弱类型),因为为了添加任何值,您需要制作两个版本的函数;我展示了一个变体,其中 LHS 值被转换并添加到 RHS 类型,但您也需要相反的方式。
而且由于我们在 Rust 中没有重载,所以这变得更不方便,你必须调用不同的函数并记住这一点。
编辑:我玩了一点,并设法为不同类型的混合使用了一个函数,但使用它迫使您每次都指定一个返回类型
struct Adder<T>(std::marker::PhantomData<T>);
impl<T> Adder<T> {
fn add<A, B>(a: A, b: B) -> T
where T: std::ops::Add<Output = T>, A: Into<T>, B: Into<T> {
return a.into() + b.into();
}
}
println!("add int int: {}", Adder::<i32>::add(1, 2));
println!("add int float: {}", Adder::<f64>::add(1, 2.3));
println!("add float int: {}", Adder::<f64>::add(2.3, 1));
Run Code Online (Sandbox Code Playgroud)
struct 的目的是将add的模板参数(可以完全推导出)与生成的模板参数(您需要指定,否则返回类型不明确)分开
| 归档时间: |
|
| 查看次数: |
645 次 |
| 最近记录: |