Rust 具有非常惊人的功能,可以使用 proc_macros 在编译时生成代码,这些代码是纯 Rust 代码。C++ 中是否有类似的东西可以让我执行真正的 C++ 代码来生成代码而不是丑陋的定义。
我是生锈新手。我正在尝试创建宏,它需要一个缓冲区,然后从中解码一些数据并创建给定的变量列表。如果发生错误,那么它应该打印错误并继续,因为我将在接收缓冲区的循环中调用它。像这样的东西:-
for bin_ref in bufs {
extract!( bin_ref anime &str episodes u32 season u32);
//if everything goes ok then do some cool stuff with
//above variables otherwise take next buf_ref
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?所以我采用了这种方法:-
#[macro_export]
macro_rules! extract {
( $buf:ident $($var:ident $typ:ty),* ) => {
$(
ext_type!( $buf $var $typ );
)*
};
}
#[macro_export]
macro_rules! ext_type {
( $buf:ident $var:ident &str ) => {
let mut $var : &str = ""; //some string specific function
println!("doing cool things with '{}' which …Run Code Online (Sandbox Code Playgroud) 我试图在发生分段错误时重新启动程序。
我有以下最小的可重现代码:-
#include <csignal>
#include <unistd.h>
#include <iostream>
int app();
void ouch(int sig) {
std::cout << "got signal " << sig << std::endl;
exit(app());
}
struct L { int l; };
static int i = 0;
int app() {
L *l= nullptr;
while(1) {
std::cout << ++i << std::endl;
sleep(1);
std::cout << l->l << std::endl; //crash
std::cout << "Ok" << std::endl;
}
}
int main() {
struct sigaction act;
act.sa_handler = ouch;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGKILL, &act, 0);
sigaction(SIGSEGV, …Run Code Online (Sandbox Code Playgroud) 我用 std::make_tuple 函数制作大元组。
像这样的东西
template <class ...T>
QCborArray array(const T&... args) {
return {args...};
}
Run Code Online (Sandbox Code Playgroud)
但用元组而不是参数包