在R6RS Scheme中是否有任何方法可以获取当前环境,然后将其作为第二个参数传递给eval
?
例如,下面的表达式返回9时问号应该是什么?
(let ((x 4)
(y 5))
(eval '(+ x y) ???))
Run Code Online (Sandbox Code Playgroud) 有没有专门化这样的模板,只有T有成员函数才能使专业化适用hash
?(注意这只是我想要做的一个例子.我知道对于每个具有hash
在operator==
成员函数中自己检查它的函数的类会更有意义,但我只是想知道这种类型事情是可能的.)
template <class T>
bool equals(const T &x, const T &y)
{
return x == y;
}
template <class T> // somehow check if T has a member function 'hash'
bool equals<T>(const T &x, const T &y)
{
return x.hash() == y.hash() && x == y;
}
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我更喜欢预C++ 11解决方案.
c++ templates template-specialization template-meta-programming
例如,我可以这样做:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Width="Auto">
<RowDefinition Width="Auto">
<RowDefinition Width="Auto">
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0">Header 1</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" MaxLines="1" />
<Button Grid.Row="0" Grid.Column="2">Send</Button>
<Button Grid.Row="0" Grid.Column="3">Save</Button>
<TextBlock Grid.Row="1" Grid.Column="0">Header 2</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" MaxLines="1" />
<Button Grid.Row="1" Grid.Column="2">Send</Button>
<Button Grid.Row="1" Grid.Column="3">Save</Button>
<TextBlock Grid.Row="2" Grid.Column="0">Header 3</TextBlock>
<TextBox Grid.Row="2" Grid.Column="1" MaxLines="1" />
<Button Grid.Row="2" Grid.Column="2">Send</Button>
<Button Grid.Row="2" Grid.Column="3">Save</Button>
</Grid>
Run Code Online (Sandbox Code Playgroud)
或者我可以这样做:
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock>Header 1</TextBlock>
<TextBox MaxLines="1" />
<Button>Send</Button> …
Run Code Online (Sandbox Code Playgroud) 我学习的方法是初始播种随机数生成器,srand(time(NULL))
然后使用调用rand()
生成随机数.这种方法的问题是如果我在同一秒内多次运行我的程序,生成的随机数将始终相同.这有什么好办法?
什么是线程安全且无内存泄漏的方法将字符串从C++返回到SWIG python接口?
SWIG会自动将char *
返回值的内容复制到python字符串中.这个SWIG指南给出了一个例子:
char *__str__() {
static char temp[256];
/* WRITE STUFF TO STRING */
return &temp[0];
}
Run Code Online (Sandbox Code Playgroud)
他们的示例使用静态字符串作为返回值,但如果我的C++程序有多个线程,它们可以轻松覆盖彼此的字符串.
返回新分配的字符串会产生内存泄漏,因为SWIG不知道要释放它.
我唯一能想到的就是注册并返回一个指向实际python字符串对象的指针(这样python垃圾收集器会照顾它),但我不知道该怎么做,我想知道是否有更简单的方法.
大部分的输出方法Go的io
包的回报(int, error)
,例如io.Writer
的Write([]byte)
方法和io.WriteString(io.Writer, string)
功能.然而,少数的输出方法,如io.WriterTo
的WriteTo
方法,返回(int64, error)
来代替.这使得不方便实现WriteTo
在以下方面Write
或WriteString
不存储的中间值,并输入从将其转换int
到int64
.造成这种差异的原因是什么?
我编译了以下C代码:
typedef struct {
long x, y, z;
} Foo;
long Bar(Foo *f, long i)
{
return f[i].x + f[i].y + f[i].z;
}
Run Code Online (Sandbox Code Playgroud)
用命令gcc -S -O3 test.c
.这是输出中的Bar函数:
.section __TEXT,__text,regular,pure_instructions
.globl _Bar
.align 4, 0x90
_Bar:
Leh_func_begin1:
pushq %rbp
Ltmp0:
movq %rsp, %rbp
Ltmp1:
leaq (%rsi,%rsi,2), %rcx
movq 8(%rdi,%rcx,8), %rax
addq (%rdi,%rcx,8), %rax
addq 16(%rdi,%rcx,8), %rax
popq %rbp
ret
Leh_func_end1:
Run Code Online (Sandbox Code Playgroud)
关于这个汇编代码,我有几个问题:
pushq %rbp
"," movq %rsp, %rbp
"和" popq %rbp
" 的目的是什么,如果既没有rbp
也没有rsp
在函数体中使用?rsi …
以下关键字的目的是什么?
and bitand compl not_eq or_eq xor_eq
and_eq bitor not or xor
Run Code Online (Sandbox Code Playgroud)
如果他们只是直接相当于:
&& & ~ != |= ^=
&= | ! || ^
Run Code Online (Sandbox Code Playgroud) 基本上我想要一个 bash 脚本进程在收到 后SIGINT
,在退出之前终止其所有子进程。我在这里读到的内容是:
trap "kill -TERM -$$ ; exit 1" INT QUIT
Run Code Online (Sandbox Code Playgroud)
我还阅读了使用kill 0
:
trap "kill -TERM 0 ; exit 1" INT QUIT
Run Code Online (Sandbox Code Playgroud)
它们之间有什么区别,它们是否满足以下要求(如果不满足,什么要求?)?:
./foo.sh &
,然后,终止./foo.sh
第二个进程或实例,即使在同一个 tty 中,终止第二个进程也不应终止第一个进程)。Go非常不幸缺乏内置断言.我想以这种方式实现它们:
const ASSERT = true
func SomeFunction() {
if ASSERT && !some_condition_that_should_always_be_true() {
panic("Error message or object.")
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我定义if语句会被优化const ASSERT = false
吗?
c++ ×3
c ×2
go ×2
.net ×1
assert ×1
bash ×1
c# ×1
environment ×1
eval ×1
gcc ×1
grid ×1
if-statement ×1
io ×1
keyword ×1
linux ×1
operators ×1
optimization ×1
output ×1
process ×1
python ×1
r6rs ×1
random ×1
scheme ×1
shell ×1
srand ×1
stackpanel ×1
string ×1
swig ×1
templates ×1
wpf ×1
x86 ×1
x86-64 ×1