我有以下代码:
#include <iostream>
#include <functional>
void f(const std::function<void()>&)
{
std::cout << "In f(const std::function<void()>&)." << std::endl;
}
void f(std::function<void()>&&)
{
std::cout << "In f(std::function<void()>&&)." << std::endl;
}
int main()
{
auto func = []() { std ::cout << "func\n"; };
f(func); // calls void f(std::function<void()>&&)
/*const*/ std::function<void()> func2 = []() { std ::cout << "func\n"; };
f(func2); // calls void f(const std::function<void()>&)
f([]() { std ::cout << "func\n"; }); // calls void f(std::function<void()>&&)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么第一次调用f(当我使用auto …
我需要探索一个目录及其所有子目录。我可以通过递归以同步方式轻松浏览目录:
use failure::Error;
use std::fs;
use std::path::Path;
fn main() -> Result<(), Error> {
visit(Path::new("."))
}
fn visit(path: &Path) -> Result<(), Error> {
for e in fs::read_dir(path)? {
let e = e?;
let path = e.path();
if path.is_dir() {
visit(&path)?;
} else if path.is_file() {
println!("File: {:?}", path);
}
}
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用tokio_fs以下异步方式执行相同操作时:
use failure::Error; // 0.1.6
use futures::Future; // 0.1.29
use std::path::PathBuf;
use tokio::{fs, prelude::*}; // 0.1.22
fn visit(path: PathBuf) -> impl Future<Item = (), Error = Error> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 tokio 复制文件以进行异步操作。我看到 tokio 没有公开任何tokio::fs::copy可以为我完成工作的方法(例如std::fs::copy同步操作的等效方法)。
在尝试实现这样的方法时,我实际上无法使用 来创建文件tokio::fs::File::create,即以下代码不会创建任何文件:
tokio::fs::File::open("src.txt")
.and_then(|mut file| {
let mut content = Vec::new();
file.read_buf(&mut content)
.map(move |_| tokio::fs::File::create("dest.txt"))
})
.map_err(Error::from)
.map(drop);
Run Code Online (Sandbox Code Playgroud)
如何复制src.txt到dest.txt使用 tokio 和 asyncfs方法?
这是游乐场的链接
dyn我可以看到返回位置中的和 (静态)特征之间的区别impl,例如:
fn foo() -> Box<dyn Trait> {}
Run Code Online (Sandbox Code Playgroud)
与
fn foo() -> impl Trait {}
Run Code Online (Sandbox Code Playgroud)
在版本中,dyn只要它们都实现了 Trait,我就可以返回不同的类型,而在版本中,impl我只允许返回相同的类型(如果我返回引用,则同样适用)。
但我看不到dyn Trait参数位置的目的,例如:
fn foo(x: &dyn Trait) {}
Run Code Online (Sandbox Code Playgroud)
与
fn foo(x: &impl Trait) {} // syntatic sugar of `fn foo<T: Trait>(x: &T){}`
Run Code Online (Sandbox Code Playgroud)
两者有什么区别?我为什么要使用其中之一?该dyn版本允许我做哪些静态版本不允许的事情(例如,我不能通过放宽 的隐式Sized限制来做到这一点?Sized)?
我有我的特征集合,我希望能够为地图中的每个项目调用特征的可变方法。
目前我按顺序执行此操作,我的收藏如下所示:
use std::cell::RefCell;
use std::collections::*;
use std::rc::Rc;
trait Trait {
fn foo_mut(&mut self);
}
fn main() {
let mut items: HashMap<i32, Rc<RefCell<dyn Trait>>> = HashMap::new();
// I have a separate data structure that holds Week<RefCell<dyn Trait>>
for item in items.values_mut() {
item.borrow_mut().foo_mut();
}
}
Run Code Online (Sandbox Code Playgroud)
我现在想并行调用 trait 方法,所以我首先将我的数据结构更改为:
use std::collections::*;
use std::sync::{Arc, RwLock};
fn main() {
let mut items: HashMap<i32, Arc<RwLock<dyn Trait>>> = HashMap::new();
for item in items.values_mut() {
item.write().unwrap().foo_mut();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我遇到了rayon,我尝试使用它的并行迭代器,但以下代码引发错误:
items.par_iter_mut().for_each(|(id, item)| item.write().unwrap().foo_mut());
Run Code Online (Sandbox Code Playgroud)
error[E0599]: no …Run Code Online (Sandbox Code Playgroud) 我有一个TextBlock地方,其Text财产与另一个属于一个价值的财产绑定:
<TextBlock Text="{Binding Path=Count}" FontWeight="Bold" />
Run Code Online (Sandbox Code Playgroud)
因此,如果(例如)Count是4的,我将在我的TextBlock数见4.
现在,如果我想在数字4之前和之后添加一些文本,我必须做什么(例如我想在方括号[4]中看到这个数字)?
谢谢.
我正在Windows Mobile 6.1上使用3.5 Compact Framework开发应用程序.我有一个ListView,我想在添加项目时自动滚动此列表.
我能怎么做?
我需要创建一个ID2D1SolidColorBrush,我有一个指针ID2D1Brush.我用我ID2D1HwndRenderTarget来创建纯色画笔:
ID2D1Brush* brush = nullptr;
myRenderTarget->CreateSolidColorBrush(color,
reinterpret_cast<ID2D1SolidColorBrush**>(&brush));
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但我想知道这是否reinterpret_cast是正确的选择.
我有以下代码:
#include <memory>
template<typename T, size_t Level>
class Foo
{
friend class Foo<T, Level + 1>;
typedef std::unique_ptr<T> ElmPtr;
typedef std::unique_ptr<Foo<ElmPtr, Level - 1>> NodePtr;
public:
Foo() {
// no errors
auto c = children;
}
Foo(int n) {
// !!! compiler error !!!
auto c = children;
}
std::array<NodePtr, 4> children;
};
template<typename T>
class Foo<T, 0>
{
friend class Foo<T, 1>;
public:
Foo() {}
};
int main()
{
Foo<int, 1> foo1;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误C2248:'std :: unique_ptr <_Ty> :: …
我重写了WndProc处理 Windows 消息的窗口的方法。
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
Run Code Online (Sandbox Code Playgroud)
为了在捕获左键按下事件 ( WM_LBUTTONDOWN) 时获取鼠标的位置,我使用以下代码:
auto x = GET_X_LPARAM(lParam);
auto y = GET_Y_LPARAM(lParam);
Run Code Online (Sandbox Code Playgroud)
这效果很好。我能够获得鼠标在窗口上的相对位置。但是当我捕获鼠标滚轮事件()时遇到问题WM_MOUSEWHEEL。在这种情况下,上面的宏似乎返回了鼠标在屏幕内的正确位置。
即使在鼠标滚轮事件上,如何才能获取鼠标的相对位置?
c++ ×4
rust ×4
c++11 ×3
asynchronous ×2
c# ×2
rust-tokio ×2
auto ×1
autoscroll ×1
binding ×1
casting ×1
direct2d ×1
directx ×1
events ×1
file ×1
filesystems ×1
iterator ×1
lambda ×1
listview ×1
mousewheel ×1
overloading ×1
templates ×1
text ×1
textblock ×1
traits ×1
unique-ptr ×1
wpf ×1