我试图理解为什么以下C++代码无法编译
int main () {
int a[10];
int (*p)[10] = &a;
int *q = static_cast<int *>(++p);
}
Run Code Online (Sandbox Code Playgroud)
如果不是很明显,我试图做的是通过使用指针算法找到指向数组末尾的指针.
到目前为止,我的理解是p具有十个整数数组的类型指针,表达式也是如此++p.通常,我可以将一个int数组分配给指向int的类型指针的变量,但这会在递增的指针上失败++p.
我第一次尝试没有,static_cast但也没有用.
我一直试图以一种非常通用的方式编写一些Rust代码,而没有明确指定类型。但是,我到达了需要将a转换usize为a的位置f64,但这不起作用。大概f64没有足够的精度来保存任意usize值。在夜间频道上进行编译时,出现错误消息:error: the trait `core::convert::From<usize>` is not implemented for the type `f64` [E0277]。
那么,如果我想编写尽可能通用的代码,那有什么选择呢?显然,我应该使用可能会失败的特征(与Into或不同From)。已经有那样的东西了吗?是否有通过实施转换的特征as?
这是下面的代码。
#![feature(zero_one)]
use std::num::{Zero, One};
use std::ops::{Add, Mul, Div, Neg};
use std::convert::{From, Into};
/// Computes the reciprocal of a polynomial or of a truncation of a
/// series.
///
/// If the input is of length `n`, then this performs `n^2`
/// multiplications. Therefore the complexity is `n^2` when the type …Run Code Online (Sandbox Code Playgroud)