我将重新开始使用c ++并考虑变量的范围.如果我在函数内部有一个变量然后我返回该变量,那么当它返回时变量不会"死",因为它所在的范围已经结束了?
我用函数返回一个字符串尝试了这个,它确实有效.有谁能解释一下?或者至少指出一些可以向我解释的地方.
谢谢
在C++中是否保证在函数中的自动变量被销毁之前创建的返回值?公告篮::获取:
class Basket
{
public:
// Gift is a struct containing safely copyable things like int or string
Gift gift;
// Used to protect access and changes to gift
Mutex mutex;
// Copy gift into present, while locked to be thread safe
void put (const Gift & gift)
{
Lock lock(mutex); // Constructor locks, destructor unlocks mutex
this->gift = gift; // Gift assignment operator
}
// Return a memberwise-copy of gift, tries to be thread safe (but is it?)
Gift …
Run Code Online (Sandbox Code Playgroud) 是get_a()
功能安全的比赛条件或者我需要明确地复制str_
在get_b()
为了有一个线程安全的功能?
class Class {
public:
auto get_a() -> std::string {
auto&& guard = std::lock_guard{mutex_};
return str_;
}
auto get_b() -> std::string {
auto&& guard = std::lock_guard{mutex_};
auto str = str_;
return str;
}
private:
std::mutex mutex_{};
std::string str_{};
};
Run Code Online (Sandbox Code Playgroud)
注意:我知道Stack Overflow上有类似的问题,但我找不到明确回答这个问题的问题.
在尝试编写一个类,调用它的构造函数和析构函数之间的持续时间时,我遇到了我认为是clang中的错误.(编辑:这不是一个bug;它是实现定义的副本省略)
timer
下面的结构保存一个指向作为引用传入的持续时间对象的指针,并将范围的持续时间添加到此.
#include <iostream>
#include <chrono>
struct timer {
using clock = std::chrono::high_resolution_clock;
using time_point = clock::time_point;
using duration = clock::duration;
duration* d_;
time_point start_;
timer(duration &d) : d_(&d), start_(clock::now()) {}
~timer(){
auto duration = clock::now() - start_;
*d_ += duration;
std::cerr << "duration: " << duration.count() << std::endl;
}
};
timer::duration f(){
timer::duration d{};
timer _(d);
std::cerr << "some heavy calculation here" << std::endl;
return d;
}
int main(){
std::cout << "function: " << f().count() << std::endl;
} …
Run Code Online (Sandbox Code Playgroud) 我正在考虑创建一个代表同步原语所有权的类,如下所示:
class CCriticalSectionLock
{
public:
CCriticalSectionLock( CCriticalSection &cs ) : cs( cs )
{ cs.Enter(); }
~CCriticalSectionLock()
{ cs.Leave(); }
private:
CCriticalSection &cs;
};
Run Code Online (Sandbox Code Playgroud)
这似乎是一种在函数期间获取所有权并确保即使存在多个退出点或例外也可以释放所有权的好方法.但是,它确实引发了一些关于编译器何时会对各种事物进行评估的细微问题.考虑以下用途:
int MyMethod( void )
{
not_locked(); // do something not under lock
CCriticalSectionLock myLock( someCriticalSection );
locked(); // do something under lock
return ...; // some expression
}
Run Code Online (Sandbox Code Playgroud)
据我所知,C++一生规则将保证not_locked()
会被调用之前采取锁,并locked()
同时持有锁会被调用.
但是,我不太清楚的是,何时返回的表达式将根据锁定析构函数的调用点进行计算.是否保证在析构函数之前计算表达式?我会这么认为,但我不是百分百肯定,如果不是,它可能导致非常微妙,间歇性,难以发现的错误!
考虑以下程序:
#include <functional>
#include <iostream>
class RvoObj {
public:
RvoObj(int x) : x_{x} {}
RvoObj(const RvoObj& obj) : x_{obj.x_} { std::cout << "copied\n"; }
RvoObj(RvoObj&& obj) : x_{obj.x_} { std::cout << "moved\n"; }
int x() const { return x_; }
void set_x(int x) { x_ = x; }
private:
int x_;
};
class Finally {
public:
Finally(std::function<void()> f) : f_{f} {}
~Finally() { f_(); }
private:
std::function<void()> f_;
};
RvoObj BuildRvoObj() {
RvoObj obj{3};
Finally run{[&obj]() { obj.set_x(5); }};
return …
Run Code Online (Sandbox Code Playgroud) c++ ×7
destructor ×3
copy-elision ×2
c++-faq ×1
clang++ ×1
locking ×1
optimization ×1
raii ×1
return ×1
return-value ×1
rvo ×1
scope ×1
stdmutex ×1