我读过它NSTemporaryDirectory()返回一个临时目录的字符串,但也可能返回nil.这是iOS中的情况吗?有没有经历过NSTemporaryDirectory()返回零?它什么时候发生?
如果我必须准备好NSTemporaryDirectory()可以返回零,那么我完全使用它是没有意义的.如果我必须提供回退机制,我可以首先使用这种机制.
那么创建临时目录的简单而安全的方法是什么?
传递函数参数是否安全getAName(getA().get())?getA()返回一个对象unique_ptr<A>.
我在VS 2010上测试下面的全部代码,它可以工作.但我想确定它是否是c ++标准,对其他c ++编译器是否安全?
#include "stdafx.h"
#include <memory>
#include <iostream>
using namespace std;
class A
{
public:
A(){ cout<<"A()"<<endl;}
~A(){ cout<<"~A()"<<endl;}
string name() { return "A"; }
};
std::unique_ptr<A> getA()
{
return std::unique_ptr<A>(new A());;
}
void getAName(A* a)
{
if(a)
{
cout << a->name().c_str() << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
getAName(getA().get());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
控制台中的输出是:
A()
A
~()
Run Code Online (Sandbox Code Playgroud)
为了所有编译器的安全,是否有必要使代码如下所示?
unique_ptr<A> a = getA();
getAName(a.get());
Run Code Online (Sandbox Code Playgroud) 我们都知道这样的事情在c ++中是有效的:
const T &x = T();
Run Code Online (Sandbox Code Playgroud)
而:
T &x = T();
Run Code Online (Sandbox Code Playgroud)
不是.
在最近的一个问题中,谈话导致了这一规则.OP发布了一些明显唤起UB的代码.但我希望它的修改版本能够工作(这是修改后的版本):
#include <iostream>
using namespace std;
class A {
public:
A(int k) { _k = k; };
int get() const { return _k; };
int _k;
};
class B {
public:
B(const A& a) : _a(a) {}
void b() { cout << _a.get(); }
const A& _a;
};
B* f() {
return new B(A(10));
}
int main() {
f()->b();
}
Run Code Online (Sandbox Code Playgroud)
这会在某些机器上打印垃圾,在其他机器上打印10个......对我来说听起来像UB :-).但后来我想,A …
例如,用户正在登录,系统正在存储关于它们的信息示例:birth date,从会话中获取此信息或查询数据库是否更快?
我的想法是,用户只需要登录一次并且会话总是在那里,但是如果我查询数据库,那么如果用户重新加载页面,系统需要反复查询,而不是从数据中获取数据临时'地方'.
我使用PHP和MySQL.
在Matlab上,当我使用"for ... end"循环时,索引变量在循环完全执行后仍然存在于我的工作区中.我希望它被自动删除,因为它在循环之外不再相关并且它污染了工作空间.
例如,在以下代码中,变量"i"在执行循环后仍然存在.因为它应该是一个局部变量,我希望它能自动删除,而不必我明确地去做.
List = [1 2 3 4] ;
for i = List
fprintf('value = %i\n', i) ;
end
% "i" still exists, while its outside of its context
clear i; % I would like to avoid doing this everytime I exit a for..end
Run Code Online (Sandbox Code Playgroud)
我知道这更像是一个美学问题,而不是一个错误,但为了更容易理解我的程序结果,我希望当我退出它们的上下文时,那些"临时"变量就会消失.
到目前为止,我只能通过重用它们来减少这些临时变量的数量.
编辑:
似乎没有真正的解决方案来自动删除那些"临时"变量.避免使用这些变量的最接近的方法是:
避免循环
在函数中创建循环,函数的变量是本地的,不会进入工作区.
代码如下:
#include <iostream>
using namespace std;
class A {
};
A rtByValue() {
return A();
}
void passByRef(A &aRef) {
// do nothing
}
int main() {
A aa;
rtByValue() = aa; // compile without errors
passByRef(rtByValue()); // compile with error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++编译器给出以下错误:
d.cpp: In function ‘int main()’:
d.cpp:19:23: error: invalid initialization of non-const reference of type ‘A&’ from an rvalue of type ‘A’
d.cpp:12:6: error: in passing argument 1 of ‘void passByRef(A&)’
Run Code Online (Sandbox Code Playgroud)
它说我不能将rvalue作为非const引用的参数传递,但是我很困惑的是为什么我可以分配给这个rvalue,就像代码所示.
我想知道关于下面这段代码的标准是什么.可以string临时对象的析构函数调用之前执行printPointer?
ps VS2010编译器不会抱怨此代码并且工作正常.
void printPointer(const string* pointer)
{
cout << *pointer << endl;
}
const string* func(const string& s1)
{
return &s1;
}
int main()
{
printPointer(func("Hello, World!!!"));
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个跨平台Path类,如:
class Path {
public:
// ...
Path parent() const; // e.g., /foo/bar -> /foo
std::string const& as_utf8() const {
return path;
}
private:
std::string path;
};
Run Code Online (Sandbox Code Playgroud)
所述parent()成员函数返回的父路径this的路径,所以它(正确)返回一个新构造的Path对象,它表示它.
对于将OS级别的路径表示为UTF-8字符串(例如,Unix)的平台,将as_utf8()引用直接返回到内部表示似乎是合理的,path因为它已经是 UTF-8.
如果我有以下代码:
std::string const &s = my_path.as_utf8(); // OK (as long as my_path exists)
// ...
Path const &parent = my_path.parent(); // OK (temporary lifetime extended)
Run Code Online (Sandbox Code Playgroud)
这两行都很好,因为:
my_path持续存在,那么s仍然有效.parent()由const&.到现在为止还挺好.但是,如果我有以下代码: …
我遇到了这个问题format!,据我所知,在一个没有锚定到任何东西的模式中创建一个临时值。
let x = 42;
let category = match x {
0...9 => "Between 0 and 9",
number @ 10 => format!("It's a {}!", number).as_str(),
_ if x < 0 => "Negative",
_ => "Something else",
};
println!("{}", category);
Run Code Online (Sandbox Code Playgroud)
在这段代码中, 的类型category是 a &str,它通过返回一个像 的文字来满足"Between 0 and 9"。如果我想使用 将匹配的值格式化为切片as_str(),则会出现错误:
let x = 42;
let category = match x {
0...9 => "Between 0 and 9",
number @ 10 => format!("It's a {}!", …Run Code Online (Sandbox Code Playgroud) 这可能是我不理解借用检查器的一些技术细节的教科书案例,但如果有人能帮我解决这个问题会很好。
我有这个(令人难以置信的简化)代码块,它编译得非常好。
pub struct Example(pub Vec<String>);
impl Example {
pub fn iter(&self) -> impl Iterator<Item=&String> {
self.0.iter()
}
}
pub fn some_condition(_: &str) -> bool {
// This is not important.
return false;
}
pub fn foo() -> bool {
let example = Example(vec!("foo".to_owned(), "bar".to_owned()));
let mut tmp = example.iter();
tmp.all(|x| some_condition(x))
}
pub fn main() {
println!("{}", foo());
}
Run Code Online (Sandbox Code Playgroud)
但是,我尝试的第一件事(在我看来,应该等同于上述内容)是tmp完全删除临时变量,如下所示
pub fn foo() -> bool {
let example = Example(vec!("foo".to_owned(), "bar".to_owned()));
example.iter().all(|x| some_condition(x))
}
Run Code Online (Sandbox Code Playgroud)
但是这个版本会产生以下错误。
pub struct …Run Code Online (Sandbox Code Playgroud) temporary ×10
c++ ×5
rust ×2
api-design ×1
c++11 ×1
const ×1
data-members ×1
database ×1
destructor ×1
for-loop ×1
ios ×1
lifetime ×1
login ×1
matlab ×1
objective-c ×1
performance ×1
reference ×1
rvalue ×1
scope ×1
session ×1
stl ×1
string ×1
unique-ptr ×1