在64位系统上,sizeof(unsigned long)取决于系统实现的数据模型,例如,它在LLP64(Windows)上为4个字节,在LP64(Linux等)上为8个字节.什么是sizeof(size_t)应该是什么?它是否随数据模型而变化sizeof(long)?如果是这样,怎么样?
参考文献:
我想迭代一个预先分配的浮点数组,该数组包含一个不拥有数据的自定义容器,但是对它的一部分起作用.例如,命名容器类LinhaSobre:
std::unique_ptr<float[]> data(new float[720]);
...
//creates container to iterate 26 floats starting from from data[12]
LinhaSobre cont(data.get()+12, 26);
//sets those elements to 1.5
for(size_t i = 0; i < cont.size(); i++)
cont[i] = 1.5f;
Run Code Online (Sandbox Code Playgroud)
这是一个可能的实现operator[]:
//...
//LinhaSobre has a member mem0 which is initialized
//as a pointer to where the interval starts
float & LinhaSobre::operator[] (size_t i)
{
return *(mem0+i);
}
Run Code Online (Sandbox Code Playgroud)
请注意,我正在返回LinhaSobre::operator[]它不拥有的数据的引用.它不应该干扰数据的生命周期(构造函数,析构函数).
现在我想暴露data另一种模式存储的std::array<float,4>,而不是纯粹的float.例如,命名新类LinhaSobre4f:
std::unique_ptr<float[]> data(new float[720]); …Run Code Online (Sandbox Code Playgroud) Rust 有async可以与Abortable期货相关联的方法。文档说,中止时:
未来将立即完成,没有任何进一步的进展。
绑定到未来的任务所拥有的变量会被删除吗?如果这些变量实现了drop,会drop被调用吗?如果未来产生了其他未来,它们是否会在一个链中流产?
例如:在下面的代码片段中,我没有看到为中止的任务发生析构函数,但我不知道它是否未被调用或发生在未显示打印的单独线程中。
use futures::executor::block_on;
use futures::future::{AbortHandle, Abortable};
struct S {
i: i32,
}
impl Drop for S {
fn drop(&mut self) {
println!("dropping S");
}
}
async fn f() -> i32 {
let s = S { i: 42 };
std::thread::sleep(std::time::Duration::from_secs(2));
s.i
}
fn main() {
println!("first test...");
let (abort_handle, abort_registration) = AbortHandle::new_pair();
let _ = Abortable::new(f(), abort_registration);
abort_handle.abort();
std::thread::sleep(std::time::Duration::from_secs(1));
println!("second test...");
let …Run Code Online (Sandbox Code Playgroud) 我最近了解到我可以这样做:
auto a = +[]{return true;};
a = +[]{return false;};
Run Code Online (Sandbox Code Playgroud)
并理解无捕获的 lambda 衰减为函数指针,正如 GCC 所证实的:
bool (*)()
但是实际的函数对象存储在哪里呢?它是如何被释放的?为什么我可以存储指向临时 lambda 的指针?我理解该语言的极端情况,其中常量引用会延长对象的生命周期,因此我预计 lambda 会衰减为此类内容,而不是原始指针。