让我们假设我有struct Foo
带移动constructor
和operator=(Foo&&)
,我用它作为数据成员:
Foo f()
{
Foo foo;
//code
return foo;
}
struct Boo {
Foo foo;
Boo() {
foo = f();//1
foo = std::move(f());//2
}
};
Run Code Online (Sandbox Code Playgroud)
在情况(2)我实际上不需要std::move
,但如果我在这里使用它,这会使一些不好的事情,如防止优化?
我读到这个:为什么std :: move会阻止RVO?
并找出改变return foo;
,以return std::move(foo);
引起致残RVO
,但怎么样(2)不会引起类似的情况?如果是这样,为什么?
我遇到了类似的问题:Android Studio 64位错误:32位Linux Android模拟器二进制文件已被删除
我在Gentoo Linux/amd64上运行android studio 2.1.1,当我尝试运行android android studio的模拟器时给我错误:
/ home/user/Android/Sdk/tools/emulator -netdelay none -netspeed full -avd Nexus_5X_API_23 ERROR:32位Linux Android模拟器二进制文件已弃用,要使用它们,您必须至少执行以下操作之一: - 使用调用'模拟器'时'-force-32bit'选项. - 在您的环境中将ANDROID_EMULATOR_FORCE_32BIT设置为"true".任何一个都允许您使用32位二进制文件,但请注意,这些将在未来的Android SDK版本中消失.在此之前考虑迁移到64位Linux系统.
但
$ file/home/user/Android/Sdk/tools/emulator/home/user/Android/Sdk/tools/emulator:ELF 64位LSB可执行文件,x86-64,版本1(SYSV),动态链接,解释器/ lib64 /ld-linux-x86-64.so.2,对于GNU/Linux 2.6.15,被剥离
正如你可以看到模拟器是64位二进制,我运行64位操作系统,还有工具 - > Android-> Avd Manger显示Nexus_5X_API_23有CPU/ABI = x86_64,
那么什么是"32位错误"呢?
更新
看起来它需要一些权限,因为我可以从root用户运行模拟器.但仍无法猜测究竟需要什么.
我想将目录名称列表传递给函数,如下所示:
use std::path::Path;
fn test(dirs: &Vec<Path>) {}
fn main() {
let dirs = vec![Path::new("/tmp"), Path::new("/var/tmp")];
test(dirs);
}
Run Code Online (Sandbox Code Playgroud)
但是它不能编译:
<anon>:3:5: 4:6 error: the trait bound `[u8]: std::marker::Sized` is not satisfied [E0277]
<anon>:3 fn test(dirs: &Vec<Path>) {
<anon>:4 }
<anon>:3:5: 4:6 help: see the detailed explanation for E0277
<anon>:3:5: 4:6 note: `[u8]` does not have a constant size known at compile-time
<anon>:3:5: 4:6 note: required because it appears within the type `std::sys::os_str::Slice`
<anon>:3:5: 4:6 note: required because it appears within the type …
Run Code Online (Sandbox Code Playgroud) 我想在new
方法内部创建一个线程,并在结构被销毁后停止它:
use std::thread;
struct Foo {
handle: thread::JoinHandle<()>,
}
impl Foo {
pub fn new(name: &str) -> Foo {
let name = name.to_string();
Foo {
handle: thread::spawn(move || {
println!("hi {}", name);
}),
}
}
pub fn stop(&mut self) {
self.handle.join();
}
}
fn main() {
let mut foo = Foo::new("test");
foo.stop();
}
Run Code Online (Sandbox Code Playgroud)
这不编译,我不明白为什么:
error[E0507]: cannot move out of borrowed content
--> <anon>:15:9
|
15 | self.handle.join();
| ^^^^ cannot move out of borrowed content
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
今后,我将实施Drop …
这段代码:
fn t(r: &[u8]) {
match r {
_ if r.iter().any(|&x| x == b';') => {}
_ => {}
}
}
Run Code Online (Sandbox Code Playgroud)
给我错误:
error[E0301]: cannot mutably borrow in a pattern guard
|
10 | _ if r.iter().any(|&x| x == b';') => {}
| ^^^^^^^^ borrowed mutably in pattern guard
Run Code Online (Sandbox Code Playgroud)
我知道我不能在匹配模式中可靠地借用,但是为什么编译器认为可以r.iter()
借用?有一种iter_mut
可变借用的单独方法.
如何在不引入单独功能的情况下检查&[u8]
包含b';'
?
这是一个最小、完整且可验证的示例:
#include <QTimer>
#include <QApplication>
#include <QtDebug>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QObject boo;
QTimer::singleShot(0, &boo, []() {
qDebug() << "hi";
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
find_package(Qt5Widgets REQUIRED)
include_directories(${Qt5Widgets_INCLUDE_DIRS})
add_executable(boo main.cpp)
qt5_use_modules(boo Widgets)
Run Code Online (Sandbox Code Playgroud)
如果我针对它运行clang-analyzer :
$ mkdir build && cd build && scan-build cmake .. && scan-build cmake --build .
...
warning: Path diagnostic report is not generated. Current output format does not support diagnostics that cross file boundaries. Refer to --analyzer-output …
Run Code Online (Sandbox Code Playgroud) 这是演示问题的玩具代码:
trait Foo {}
trait Boo<T> {
fn f() -> T;
}
impl<T> Boo<T> for i32
where
T: Foo,
{
fn f() -> T {
unimplemented!();
}
}
impl<'a, T> Boo<&'a T> for i32
where
T: Foo,
{
fn f() -> T {
unimplemented!();
}
}
Run Code Online (Sandbox Code Playgroud)
我想有两个通用实现trait Boo
,但它不编译:
error[E0119]: conflicting implementations of trait `Boo<&_>` for type `i32`:
--> src/main.rs:16:1
|
7 | / impl<T> Boo<T> for i32
8 | | where
9 | | T: Foo,
10 | …
Run Code Online (Sandbox Code Playgroud) gcc 时不时地通过 may-uninitialized 发出有关 boost::optional 工作的警告,但我认为这是误报(如此处描述的https://github.com/boostorg/optional/issues/72)。
但是现在valgrind在运行时报告了同样的问题,所以出了点问题,不知道是什么问题:
//Foo.hpp
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <cstdio>
#include <string>
extern "C" {
typedef struct FooOpaque FooOpaque;
FooOpaque *Foo_new();
void Foo_delete(const FooOpaque *self);
}
class Foo {
public:
explicit Foo(FooOpaque *o) noexcept : self_(o) {}
Foo(const Foo &) = delete;
Foo &operator=(const Foo &) = delete;
Foo(Foo &&o) noexcept : self_(o.self_) { o.self_ = nullptr; }
Foo &operator=(Foo &&o) noexcept {
assert(this != &o);
free_mem(this->self_);
self_ = o.self_;
o.self_ = nullptr;
return …
Run Code Online (Sandbox Code Playgroud) 使用新版本的Rust,您可以简化结构初始化,如下所示:
Foo {
a: a,
b: b,
}
Run Code Online (Sandbox Code Playgroud)
对此
Foo { a, b }
Run Code Online (Sandbox Code Playgroud)
是否可以对format!
/ println!
-like宏做类似的事情?
现在,我需要这样写:
let a = "a";
let b = "b";
write!(file, "{a} is {b}", a = a, b = b).unwrap();
Run Code Online (Sandbox Code Playgroud)
是否可以使用这样的API编写自己的宏:
let a = "a";
let b = "b";
my_write!(file, "{a} is {b}", a, b).unwrap();
Run Code Online (Sandbox Code Playgroud) 如果我这样指定panic
,它适用于所有目标:
[profile.release]
panic = "abort"
Run Code Online (Sandbox Code Playgroud)
我只想panic = "abort"
为target=arm-linux-androideabi
.