【发布时间】:2014-11-23 02:17:37
【问题描述】:
我偶然发现了这个Rust example in Wikipedia,我想知道是否可以将其转换为语义等效的 C++ 代码?
程序定义了一个递归数据结构并在其上实现方法。递归数据结构需要一个间接层,它由一个唯一指针提供,通过box 运算符构造。 (这些类似于 C++ 库类型std::unique_ptr,尽管有更多的静态安全保证。)
fn main() {
let list = box Node(1, box Node(2, box Node(3, box Empty)));
println!("Sum of all values in the list: {:i}.", list.multiply_by(2).sum());
}
// `enum` defines a tagged union that may be one of several different kinds
// of values at runtime. The type here will either contain no value, or a
// value and a pointer to another `IntList`.
enum IntList {
Node(int, Box<IntList>),
Empty
}
// An `impl` block allows methods to be defined on a type.
impl IntList {
fn sum(self) -> int {
match self {
Node(value, next) => value + next.sum(),
Empty => 0
}
}
fn multiply_by(self, n: int) -> Box<IntList> {
match self {
Node(value, next) => box Node(value * n, next.multiply_by(n)),
Empty => box Empty
}
}
}
显然在 C++ 版本中,Rusts enum 应该替换为 union,Rusts Box 应该替换为 std::unique_ptr 并且 Rusts Node tuple 应该是 std::tuple 类型,但我只是无法理解如何用 C++ 编写等效的实现。
我知道这可能不实用(而且绝对不是用 C++ 做事的正确方法),但我只是想看看这些语言的比较(C++11 的功能足够灵活,可以进行这种修补吗?)。我还想比较编译器生成的程序集的语义等效实现(如果可能的话)。
【问题讨论】:
-
您是想用某种有效的 C++ 代码“按下”它,还是希望类似 C++ 的 C++ 代码做同样的事情?不知何故,你说的是两件事。
-
是的,这是可能的,但是没有模式匹配,这很难看:coliru.stacked-crooked.com/a/4e4c80512fa9d739(当然有 C++ 中的模式匹配库。我在这里用“可怕的访问者模式”替换它。)
-
什么是类 C++ 的 C++ 代码?通过“语义等价”,我的意思是 C++ 版本应该使用具有元组(整数值和下一个节点)和“空”指针作为其成员的联合来实现递归数据结构。当然为结构实现 sum() 和 multiply_by() 。这只是假设的示例,因此 C++ 版本可能既不惯用也不实用。
-
当然,您也可以在 C++ 中编写稍有不同的代码,以完全避免模式匹配/访问者/联合的事情:coliru.stacked-crooked.com/a/2985c5a6c47c176f(尽管这要求
std::pair与不完整的类型一起使用,这不保证) -
@dyp [res.on.functions]/2? UB?
标签: c++ c++11 rust recursive-datastructures