exception-safety Questions

2

Solved

db, err := sql.Open("postgres", "…") if err != nil { log.Fatalln(err) } defer db.Close() tpl, err := template.ParseGlob("") if err != nil { log.Fatalln(err) } If template.ParseGlob("") returns...
Martellato asked 26/7, 2013 at 18:35

14

Solved

I'm attempting to write a simple ScopeGuard based on Alexandrescu concepts but with c++11 idioms. namespace RAII { template< typename Lambda > class ScopeGuard { mutable bool committed;...
Titanesque asked 22/4, 2012 at 17:35

1

Solved

I'm using GCC 7.3.1, but also tested on coliru, which I believe is version 9.2.0. Build with the following: g++ -fsanitize=address -fno-omit-frame-pointer rai.cpp Here's rai.cpp: #include <i...
Triploid asked 12/12, 2019 at 14:29

3

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...
Simsar asked 17/2, 2018 at 15:17

2

Solved

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, whi...
Vermiculation asked 17/2, 2018 at 14:33

2

While writing some code targeting C++17, I kind of hit a stumbling block determining the exception-safety of an operation merging two compatible std::unordered_maps. Per the current working draft, ...
Untune asked 13/6, 2017 at 19:47

3

Solved

Just to clarify, using make_unique only adds exception safety when you have multiple allocations in an expression, not just one, correct? For example void f(T*); f(new T); is perfectly exceptio...
North asked 20/10, 2013 at 0:17

4

Solved

In Herb Sutter's book Exceptional C++ (1999), he has words in item 10's solution: "Exception-unsafe" and "poor design" go hand in hand. If a piece of code isn't exception-safe, that's generally ...
Embellish asked 18/8, 2012 at 1:47

7

Solved

Exceptions in C++ don't need to be caught (no compile time errors) by the calling function. So it's up to the developer's judgment whether to catch them using try/catch (unlike in Java). Is there a...
Cupreous asked 4/8, 2008 at 10:1

4

Solved

Within the scope of a member function, I want to temporarly set a member variable to a certain value. Then, when this function returns, I want to reset this member variable to a given known value....
Wispy asked 15/4, 2016 at 10:10

1

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): templ...
Abbreviated asked 10/8, 2015 at 14:49

2

Solved

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 impl...
Red asked 4/9, 2014 at 13:53

2

Solved

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 ...
Bellanca asked 15/5, 2013 at 15:20

1

Solved

std::tie returns a tuple of references, so you can do the following: int foo, bar, baz; std::tie(foo, bar, baz) = std::make_tuple(1, 2, 3); This is similar to foo, bar, baz = (1, 2, 3) in Python...
Shive asked 8/12, 2012 at 15:49

1

Solved

My understanding is that C++ implicitly generated assignment operator does a member-wise copy (this seems confirmed also by this answer). But, if during a member copy an exception is thrown (e.g. b...
Tarryn asked 12/11, 2012 at 9:48

1

Solved

Consider the following: std::vector<std::unique_ptr<int>> ptrsToInts; ptrsToInts.emplace_back(new int); If reallocation occurs in the vector, and that fails (throwing std::bad_alloc)...
Knudsen asked 1/11, 2012 at 7:40

3

Solved

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 sec...
Arabist asked 28/7, 2012 at 7:12

3

Solved

I keep reading that swap() operation, like this for example: template<class T> void swap (T &a, T &b) { T temp (a); a = b; b = temp; } is problematic when we are dealing with E...
Impassible asked 14/7, 2012 at 17:15

4

Solved

In an old blog entry titled Cleaner, more elegant, and harder to recognize, the author states: In C++ it's not quite so bad because C++ exceptions are raised only at specific points during execu...
Bespatter asked 14/6, 2012 at 8:29

4

Solved

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-swa...
Angloamerican asked 28/3, 2012 at 14:30
1

© 2022 - 2024 — McMap. All rights reserved.