exception-safety

C++ exception safety paranoia: how much is too much?

六眼飞鱼酱① 提交于 2019-12-11 01:35:00
问题 The strong exception safety guarantee says that an operation won't change any program state if an exception occurs. An elegant way of implementing exception-safe copy-assignment is the copy-and-swap idiom. My questions are: Would it be overkill to use copy-and-swap for every mutating operation of a class that mutates non-primitive types? Is performance really a fair trade for strong exception-safety? For example: class A { public: void increment() { // Copy A tmp(*this); // Perform throwing

Sink arguments and move semantics for functions that can fail (strong exception safety)

时间秒杀一切 提交于 2019-12-06 19:48:02
问题 I have a function that operates on a big chunk of data passed in as a sink argument. My BigData type is already C++11-aware and comes with fully functional move constructor and move assignment implementations, so I can get away without having to copy the damn thing: Result processBigData(BigData); [...] BigData b = retrieveData(); Result r = processBigData(std::move(b)); This all works perfectly fine. However, my processing function may fail occasionally at runtime resulting in an exception.

Sink arguments and move semantics for functions that can fail (strong exception safety)

為{幸葍}努か 提交于 2019-12-05 01:08:52
I have a function that operates on a big chunk of data passed in as a sink argument. My BigData type is already C++11-aware and comes with fully functional move constructor and move assignment implementations, so I can get away without having to copy the damn thing: Result processBigData(BigData); [...] BigData b = retrieveData(); Result r = processBigData(std::move(b)); This all works perfectly fine. However, my processing function may fail occasionally at runtime resulting in an exception. This is not really a problem, since I can just fix stuff and retry: BigData b = retrieveData(); Result

Locking a mutex in a destructor in C++11

拜拜、爱过 提交于 2019-12-04 08:41:15
问题 I have some code which need to be thread safe and exception safe. The code below is a very simplified version of my problem : #include <mutex> #include <thread> std::mutex mutex; int n=0; class Counter{ public: Counter(){ std::lock_guard<std::mutex>guard(mutex); n++;} ~Counter(){ std::lock_guard<std::mutex>guard(mutex);//How can I protect here the underlying code to mutex.lock() ? n--;} }; void doSomething(){ Counter counter; //Here I could do something meaningful } int

std::make_shared() change in C++17

巧了我就是萌 提交于 2019-12-03 04:25:13
In cppref , the following holds until C++17: code such as f(std::shared_ptr<int>(new int(42)), g()) can cause a memory leak if g gets called after new int(42) and throws an exception, while f(std::make_shared<int>(42), g()) is safe, since two function calls are never interleaved. I'm wondering which change introduced in C++17 renders this no longer applicable. The evaluation order of function arguments are changed by P0400R0 . Before the change, evaluation of function arguments are unsequenced relative to one another. This means evaluation of g() may be inserted into the evaluation of std:

Where can I find all the exception guarantees for the Standard Containers and Algorithms?

北慕城南 提交于 2019-12-03 04:17:07
问题 Yes, I've looked at the C++ standards that I could find (or the drafts), but I'm not finding any comprehensive of the exception guarantees given by STL containers. All I can find are occasional sections with incomplete descriptions on some of the functions for some of the types. Or perhaps it's there but I'm just not finding it, I don't know. Note: I'm not asking for a list of all the guarantees people can think of, which is basically in this question. I'm looking for the authoritative source

Locking a mutex in a destructor in C++11

佐手、 提交于 2019-12-03 00:36:17
I have some code which need to be thread safe and exception safe. The code below is a very simplified version of my problem : #include <mutex> #include <thread> std::mutex mutex; int n=0; class Counter{ public: Counter(){ std::lock_guard<std::mutex>guard(mutex); n++;} ~Counter(){ std::lock_guard<std::mutex>guard(mutex);//How can I protect here the underlying code to mutex.lock() ? n--;} }; void doSomething(){ Counter counter; //Here I could do something meaningful } int numberOfThreadInDoSomething(){ std::lock_guard<std::mutex>guard(mutex); return n;} I have a mutex that I need to lock in the

Where can I find all the exception guarantees for the Standard Containers and Algorithms?

泄露秘密 提交于 2019-12-02 17:35:46
Yes, I've looked at the C++ standards that I could find (or the drafts), but I'm not finding any comprehensive of the exception guarantees given by STL containers. All I can find are occasional sections with incomplete descriptions on some of the functions for some of the types. Or perhaps it's there but I'm just not finding it, I don't know. Note: I'm not asking for a list of all the guarantees people can think of, which is basically in this question . I'm looking for the authoritative source of this information itself -- or preferably, a free version of the source (e.g. a draft of the

How do I run a cleanup code on the function exit?

安稳与你 提交于 2019-12-02 06:44:33
C++ classes provide RAII idiom. Therefore you don't have to care about exceptions: void function() { // The memory will be freed automatically on function exit std::vector<int> vector(1000); // Do some work } But if you have (for some reasons) to use some pure C API, you have either to create C++ wrappers around it or to use try/catch blocks void function() { int *arr = (int*)malloc(1000*sizeof(int)); if (!arr) { throw "cannot malloc"; } try { // Do some work } catch (...) { free(arr); // Free memory in case of exception throw; // Rethrow the exception } // Free memory in case of success free

C++: why this simple Scope Guard works?

喜夏-厌秋 提交于 2019-11-30 19:36:00
Every looked at scope guard so far has a guard boolean variable. For example, see this discussion: The simplest and neatest c++11 ScopeGuard But a simple guard works (gcc 4.9, clang 3.6.0): template <class C> struct finally_t : public C { finally_t(C&& c): C(c) {} ~finally_t() { (*this)(); } }; template <class C> static finally_t<C> finally_create(C&& c) { return std::forward<C>(c); } #define FINCAT_(a, b) a ## b #define FINCAT(a, b) FINCAT_(a, b) #define FINALLY(...) auto FINCAT(FINALY_, __LINE__) = \ finally_create([=](){ __VA_ARGS__ }) int main() { int a = 1; FINALLY( std::cout << "hello" <