raii Questions

4

Solved

For many RAII "guard" classes, being instantiated as anonymous variables does not make sense at all: { std::lock_guard<std::mutex>{some_mutex}; // Does not protect the scope! // The...
Dumah asked 30/11, 2016 at 9:15

3

Solved

In normal C++ design, most objects can be deleted either by a delete statement, the free function, or a library-specific equivalent to free. For such objects, the unique_ptr Deleter implementation ...
Pharisee asked 10/10, 2016 at 20:30

5

Solved

Why do C++ Standard Library streams use open()/close() semantics decoupled from object lifetime? Closing on destruction might still technically make the classes RAII, but acquisition/release indepe...
Semiotics asked 2/9, 2014 at 13:36

1

Solved

These are the ways I know of to create singletons in Rust: #[macro_use] extern crate lazy_static; use std::sync::{Mutex, Once, ONCE_INIT}; #[derive(Debug)] struct A(usize); impl Drop for A { fn...
Funda asked 10/9, 2016 at 13:17

2

Resource Acquisition is Initialization (RAII) is commonly used in C++ to manage the lifetimes of resources which require some manner of cleanup code at the end of their lifetime, from deleteing new...
Kieserite asked 27/8, 2016 at 2:10

5

Solved

While learning C++11, I was surprised by the way moved objects appear to behave. Consider this code: #include <exception> #include <iostream> #include <type_traits> class Moveab...
Adamo asked 10/7, 2016 at 0:10

0

I'm trying to understand the difference between C++ like RAII and Obj-C or Swift like ARC. Let's say I have this function: foo() { bar = new obj(); } // lifetime of bar object ends If obj was ...
Jeu asked 5/7, 2016 at 15:6

5

Solved

I like the idea of const member variables especially when I wrap C functions into classes. The constructor takes a resource handle (e.g. a file descriptor) that stays valid during the whole object ...
Sanbo asked 11/6, 2011 at 17:25

2

Solved

In the c++ language, there are multiple ways how to open and operate on a file. However the RAII-approach is very popular, since the destruktor takes care about freeing memory. But what about the ...
Iquique asked 31/5, 2016 at 9:23

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

2

Solved

Let say I have RAII class: class Raii { Raii() {}; ~Raii() { if (<something>) throw std::exception(); } }; And if I have the function: void foo() { Raii raii; if (something) { t...
Crackup asked 2/4, 2016 at 1:4

3

Solved

The difference between those two is not clear for me, yet. What I have read about them have been very massive and complex (as the C++ is). For example, this one which belongs to years ago and from ...
Fractionate asked 8/3, 2016 at 5:49

1

Solved

A situation I often come up against is having a set of classes, Base and Derived, where the Base class has ownership of a base-class member BaseMember, and the Derived class has a reference or poin...
Trio asked 10/2, 2016 at 22:4

3

Solved

OK so if I am using a RAII idiom to manage some context attribute*, will it work as I expect if I use it nakedly in at the start of a try block? In other words, if I have this: struct raii { ra...
Nosy asked 20/1, 2016 at 18:23

3

In C++ I often use RAII-style objects to make code more reliable and allocate them on stack to make code more performant (and to avoid bad_alloc). But creating an object of concrete class on stac...
Aguiar asked 18/10, 2011 at 12:10

2

Solved

If the resource one requires to acquire in the construction of an object can fail is it possible to do RAII, if exceptions are prohibited by a local coding standard? If so, what is the canonical m...
Soilure asked 23/10, 2015 at 11:10

3

Here's a code from Stroustrup's "The C++ Programming Language" that implements a finally which I cannot quiet understand where the destructor gets called. template<typename F> struct Final_a...
Mulvey asked 17/9, 2015 at 14:14

5

Solved

Question: Is using exceptions the proper way to terminate my program if all I want is to display an error message and close (accounting that I may be deep in the program)? Or can I just explicitly ...
Harri asked 27/8, 2015 at 19:22

2

Solved

What is the most elegant solution for calling a function automatically when leaving a scope? My current approach (see below) works but I guess there should be something more general as writing a cu...
Tunny asked 26/3, 2014 at 9:26

1

I want to write a simple, small C++ RAII wrapper over a C socket. The question is in what state is a socket considered initialized (from the perspective of RAII) and so eligible for release. For ...
Ate asked 27/4, 2015 at 17:38

2

Solved

At first glance, it seems like Python's __del__ special method offers much the same advantages a destructor has in C++. But according to the Python documentation (https://docs.python.org/3.4/refere...
Twopenny asked 3/2, 2015 at 14:10

2

Solved

It's more a philosophical type of question. In C++ we have nice shiny idiom - RAII. But often I see it as incomplete. It does not well aligns with the fact that my application can be killed with S...
Corliss asked 11/9, 2014 at 7:16

2

Solved

This is about wrapping the exception handling logic in some sort of class. While writing c++ code, many time we need to catch many type/variants of exception depending on what client throw. This le...
Hydrobomb asked 30/8, 2014 at 5:26

2

Solved

Here's an issue I often run into with RAII. I was wondering if anyone had a good solution for it. Start with your standard RAII utility class: class RAIIHelper { RAIIHelper() { AcquireAResource...
Sentimentalize asked 14/7, 2014 at 18:30

4

Solved

Pardon me if this question is too silly. The most common example of usefulness of using RAII is : void func(){ // create some object pointer using any smart pointer // do some operation that may...
Slinkman asked 8/5, 2014 at 15:32

© 2022 - 2024 — McMap. All rights reserved.