小编Ale*_*nov的帖子

将shared_ptr设置为new_pointer,即old_pointer + offset

这是一个智能指针:std::shared_ptr<char> p(new char[size])表示填充原始二进制文件内容的数组.在整个数组从文件复制到RAM之后(并且仅在之后),我可以解析它,在此期间我检索一些头信息(几个第一个dwords).然后是实际数据.

在没有提供更多上下文的情况下,将所提到的共享指针设置为实际数据开头的新地址非常方便.该地址仍处于已分配的内存中.但如何设置而不失去它?

一个问题是(是/否):是否可以设置p偏移主流指针,而无需调用数据删除?

c++ smart-pointers shared-ptr pointer-arithmetic

30
推荐指数
1
解决办法
1037
查看次数

new []是否连续分配内存?

当我使用new []关键字(或new-operator)时,它是否连续分配内存?

int* arr = new int[10];
Run Code Online (Sandbox Code Playgroud)

我的意思是,有没有保证,arr [0]arr [1]紧密放置,我可以使用指针增量迭代arr?如果是这样,这种行为是否保存结构和类而不是int?

c++ new-operator

8
推荐指数
1
解决办法
343
查看次数

写入 websocket 流

tungstenite 我正在使用此示例连接到 websocket 服务器并从那里使用write.send

let connect_addr = env::args()
    .nth(1)
    .unwrap_or_else(|| panic!("this program requires at least one argument"));

let url = url::Url::parse(&connect_addr).unwrap();

let (stdin_tx, stdin_rx) = futures_channel::mpsc::unbounded();
tokio::spawn(read_stdin(stdin_tx));

let (ws_stream, _) = connect_async(url).await.expect("Failed to connect");
println!("WebSocket handshake has been successfully completed");

let (write, read) = ws_stream.split();

## THIS DOESNT WORK ###
write.send(Message::Text(format!("{}", "HELLO!")))
            .await
            .expect("Failed to send message");
Run Code Online (Sandbox Code Playgroud)

我无法让写入工作。我得到:

error[E0599]: no method named `send` found for struct `futures_util::stream::stream::split::SplitSink<tokio_tungstenite::WebSocketStream<tokio_tungstenite::stream::Stream<tokio::net::tcp::stream::TcpStream, tokio_native_tls::TlsStream<tokio::net::tcp::stream::TcpStream>>>, tungstenite::protocol::message::Message>` in the current scope
   --> src/main.rs:28:15
    | …
Run Code Online (Sandbox Code Playgroud)

rust rust-tokio

6
推荐指数
0
解决办法
2351
查看次数

选项&lt;T&gt;的map_err

因此Result<T, E>有一个非常简洁的方法map_err,允许以功能性的方式处理错误:

use std::io::Result;
use std::error::Error;
use std::string::ToString;
use std::io;

fn init() -> Result<u32> { Ok(42) }

fn do_work(_data: u32) -> Result<()> { Err(io::Error::new(io::ErrorKind::Other, "IO Error!")) }

fn handle_error<E: Error + ToString>(error: E, message: &str) -> E {
    eprintln!("{}: {}", message, error.to_string());
    error
}

fn main() {
    let _ = init()
        .map_err(|e| handle_error(e, "Init error"))
        .and_then(do_work)
        .map_err(|e| handle_error(e, "Work error")); // "Work error: IO error"
}
Run Code Online (Sandbox Code Playgroud)

如果有相同的功能风格来处理,那就太酷了Option<T>::None

use std::io::Result;
use std::error::Error;
use std::string::ToString;
use …
Run Code Online (Sandbox Code Playgroud)

error-handling rust

5
推荐指数
1
解决办法
3874
查看次数

golang中批量更新查询的最快方法

如何在 Golang 中执行批量更新查询?例如,我想在一个查询中执行这些查询。

update product set product_name='AAAA' where product_id='1'

update product set product_name='BBB' where product_id='2'

update product set product_name='CCC' where product_id='3'

update product set product_name='DDDD' where product_id='4'
Run Code Online (Sandbox Code Playgroud)

sql database go

5
推荐指数
0
解决办法
3221
查看次数

在执行过程中中断指令

假设CPU正在运行汇编指令,例如,FOO将在几个时钟(例如10)中执行

中断请求刚好在执行过程中FOO,处理器需要中断.它是否等待命令正确执行,或FOO中止并将重新启动?考虑到不同类型的中断优先级,它的行为是否有所不同?

x86 assembly interrupt

4
推荐指数
1
解决办法
226
查看次数

无条件匹配模板

我想调用模板函数,即使std::enable_if_t没有解析为true. 它将允许我在其他上下文中重用我现有的模板之一。

最小的例子:

#include <iostream>
#include <type_traits>

template<typename T, 
         typename = typename std::enable_if<std::is_same<T, int>::value>::type>
auto foo(T t) {
    std::cout << t << std::endl;
    return t;
}

template<typename T>
auto bar(T t) {
    foo(t);
}

int main() {
    int i = 42;
    foo(i); // ok already
    // foo("42"); // shouldn't
    bar("42"); // should
}
Run Code Online (Sandbox Code Playgroud)

I tried to solve it bydeclaring a bool in template argument list of foo and specify it when calling foo in bar, …

c++ templates c++17

1
推荐指数
1
解决办法
56
查看次数

如何解决 Rust 在方法设计中的“使用移动值”?

我有一个系统,它将连接多个 API 下游。我想用

let mut system = System::new()
Run Code Online (Sandbox Code Playgroud)

引入配置并进行验证,然后用于 system.init() 初始化下游的所有连接。连接所有下游后,我想通过多种方法对下游进行CRUD。

这里是游乐场

struct Conn {
    connection: String,
}
impl Conn {
    fn fetch(&mut self) -> &str {
        self.connection.push_str("data");
        &self.connection
    }
}

struct System {
    downstream: Option<Conn>,
}

impl System {
    fn new() -> Self {
        System { downstream: None }
    }

    fn init(&mut self) {
        self.downstream = Some(Conn {
            connection: String::from("db connection"),
        })
    }

    fn method_a(self) -> String {
        let mut conn = self.downstream.unwrap();
        String::from(conn.fetch())
    }
    fn method_b(self) -> …
Run Code Online (Sandbox Code Playgroud)

rust

0
推荐指数
1
解决办法
113
查看次数