这个问题与我关于这个问题的新发现密切相关.
有没有什么办法来保存的数据流中的数据php://memory或php://temp手柄之间?我读过(我无法获取的某个地方)上述流的后续开放清除了现有数据.
$mem1 = fopen('php://memory', 'r+');
fwrite($mem1, 'hello world');
rewind($mem1);
fpassthru($mem1); // "hello world"
$mem2 = fopen('php://memory', 'r+');
rewind($mem2);
fpassthru($mem2); // empty
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,无论如何,在创建新句柄时,是否还要强制现有数据保持在流中?
(后者调用fpassthru()当然会转储,hello world因为这是可能的)
这个问题询问所有临时值是否都是右值.
答案是否定的,因为如果我们考虑这个表达式:
const int &ri = 2 + 3;
Run Code Online (Sandbox Code Playgroud)
那么,同样的临时(2 + 3)值(这里是一个右值)可以用作后续表达式中的左值:
const int *pi = &ri;
Run Code Online (Sandbox Code Playgroud)
所以这个临时不是(仅)一个右值.
然后逻辑语句temporary ==> rvalue是错误的.
但是,我们不能写
const int &ri = &(2 + 3); // illegal, 2 + 3 -> temporary -> rvalue
Run Code Online (Sandbox Code Playgroud)
要么
int *i = &4; // illegal, 4 is an rvalue (literal)
Run Code Online (Sandbox Code Playgroud)
要么
int foo();
int *i = &foo(); // illegal, foo() -> temporary -> rvalue
Run Code Online (Sandbox Code Playgroud)
因此我的问题是,我们可以在某个表达式中生成一个rvalue而不需要临时或文字吗?是rvalue ==> (temporary or literal)真的吗?
如果我有一个列表理解
[mymap.get(x, None) for x in oldlist if mymap.get(x,None)]
Run Code Online (Sandbox Code Playgroud)
有没有办法mymap.get(x,None)只在这里做一次?
我想像
[y for x in oldlist if mymap.get(x,None) as y]
Run Code Online (Sandbox Code Playgroud)
但是目前这是SyntaxErrorpy 2.x中的。我希望能够引用列表理解的“表达式”或它的“ list_if”部分的结果值。
我也尝试过
[_ for x in oldlist if mymap.get(x,None)]
Run Code Online (Sandbox Code Playgroud)
但这是一个NameError,我想_只是在线上的某些解释器功能。
编辑
有没有办法以某种方式引用此临时/匿名/未命名变量,而无需重新列出该列表?
假设我有一个名为Foo的类,带有复制赋值运算符,我认为这样:
Foo() = Foo();
Run Code Online (Sandbox Code Playgroud)
是不被允许的,因为我认为(对不起,再次)Foo()是一个右值(所以我不能分配给它),是一个临时对象.显然,我试图使这个代码工作,它正常工作,显示一些字符串,以验证我的程序正确使用复制赋值运算符.
我错了吗?或者这是一种错误?有什么用处?
在这段代码中:
const int & fun(const int &i)
{
return 2*i;
}
int main()
{
const int k=3;
cout<<fun(k)<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,参数fun不是本地的(没有临时对象存储引用返回类型)那么为什么会出现此警告?
+如果我删除常量从返回类型函数fun,它说
错误:从'int'类型的右值开始,无效初始化'int&'类型的非const引用
但是在删除2*(只剩下我返回的值时)它没有显示任何错误 - >我能想到的是这个2*的东西是将返回转换为Rvalue但是后来不是Rvalue本身的返回值?我哪里错了?
我有一个用RAII编写的Array类(为了这个例子的目的,超简化):
struct Array
{
Array(int size) {
m_size = size;
m_data = new int[m_size];
}
~Array() {
delete[] m_data;
}
int* m_data = nullptr;
int m_size = 0;
};
Run Code Online (Sandbox Code Playgroud)
然后我有一个函数,它接受一个数组的引用并对它进行一些操作.我使用临时数组temp来执行处理,因为有几个原因我不能直接使用引用.完成后,我想将数据从临时数据传输到真实数据:
void function(Array& array)
{
Array temp(array.m_size * 2);
// do heavy processing on `temp`...
array.m_size = temp.m_size;
array.m_data = temp.m_data;
}
Run Code Online (Sandbox Code Playgroud)
显而易见的问题是temp在函数末尾超出范围:它的析构函数被触发,而后者又删除了内存.这种方式array将包含不存在的数据.
那么将数据所有权从一个对象"移动"到另一个对象的最佳方法是什么?
我无法调用Foo::new(words).split_first()以下代码
fn main() {
let words = "Sometimes think, the greatest sorrow than older";
/*
let foo = Foo::new(words);
let first = foo.split_first();
*/
let first = Foo::new(words).split_first();
println!("{}", first);
}
struct Foo<'a> {
part: &'a str,
}
impl<'a> Foo<'a> {
fn split_first(&'a self) -> &'a str {
self.part.split(',').next().expect("Could not find a ','")
}
fn new(s: &'a str) -> Self {
Foo { part: s }
}
}
Run Code Online (Sandbox Code Playgroud)
编译器会给我一个错误信息
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:8:17
| …Run Code Online (Sandbox Code Playgroud) 我想在以下代码中摆脱变量temp:
type myinterface interface {
f1()
}
type a struct {
val int
}
type b struct {
mi *myinterface
}
func (a) f1() {
}
func demo() {
a1 := a{3}
var temp myinterface = a1
b1 := b{&temp}
fmt.Println(b1)
Run Code Online (Sandbox Code Playgroud)
但如果我试着写
b1 := b{&myinterface(a1)}
Run Code Online (Sandbox Code Playgroud)
我收到了消息
不能取myinterface的地址(a1)(undefined)
这样做的正确方法是什么?
更新:
我没有指向接口的指针,因为接口可以包含结构或指向结构的指针,这也在这个问题中详述:
我在初始化全局变量时遇到了麻烦。我的 C++ 有点生疏,所以我不记得我的代码不起作用的原因。
文件.cpp
const char * write_path = &(std::string(getenv("FIFO_PATH")) + "/pythonread_fifo")[0];
int main(int argc, char**argv)
{
std::cout << "printing a string: ";
std::cout << (std::string(getenv("FIFO_PATH")) + "/pythonread_fifo\n");
std::cout << "printing a const char*: ";
std::cout << &(std::string(getenv("FIFO_PATH")) + "/pythonread_fifo")[0] << std::endl;
std::cout << "printing write_path:";
std::cout << write_path;
std::cout << write_path << std::endl;
std::cout << "printing FIFO_PATH:" << std::string(getenv("FIFO_PATH"));
}
Run Code Online (Sandbox Code Playgroud)
作为前提:FIFO_PATH 已正确添加到 bashrc,并且它可以工作,但是,当我启动此程序时,这是输出:
printing a string: /home/Processes/FIFOs/pythonread_fifo
printing a const char*: /home/Processes/FIFOs/pythonread_fifo
printing write_path:
printing FIFO_PATH:/home/Processes/FIFOs
Run Code Online (Sandbox Code Playgroud)
如您所见write_path,完全是空的。 …