raii

What is the lifetime of the arguments of std::async?

感情迁移 提交于 2019-12-10 14:55:29
问题 It appears that arguments of a function executed via std::async share the lifetime of the future: #include <iostream> #include <future> #include <thread> struct S { S() { std::cout << "S() " << (uintptr_t)this << std::endl; } S(S&& s) { std::cout << "S(&&) " << (uintptr_t)this << std::endl; } S(const S& s) = delete; ~S() { std::cout << "~S() " << (uintptr_t)this << std::endl; } }; int main() { { std::cout << "enter scope" << std::endl; auto func = [](S&& s) { std::cout << "func " << (uintptr

Can inversion of control and RAII play together?

醉酒当歌 提交于 2019-12-10 14:24:03
问题 I was just reading up on inversion of control (IOC) and it bothered me that it seems like it makes memory management a pain. Of course it seems ioc is mostly used in garbage collected environments (Net,Java,Scripting), while my concern is in non-gc settings. My concern here is that IOC in a way goes against RAII, as we decouple resource lifetime from object lifetime. Doesn't this added complexity bother anyone else? And the real question, what techniques can be used to make things go smoothly

Resource Aquisition Is Initialization, in Python

不打扰是莪最后的温柔 提交于 2019-12-10 14:23:26
问题 I am new to Python. I come from C++. In some code reviews, I've had several peers wanting me to move things from init and del to a start and stop method. Most of them time, this goes against the RAII that was beaten into my head with decades of C++. https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization Is RAII not a thing in Python? Shouldn't it be? After all, we can throw exceptions and we'd want to release resources when we do, no? If it isn't. Can someone give some insight

Is there a better deterministic disposal pattern than nested “using”s?

倖福魔咒の 提交于 2019-12-09 14:07:44
问题 In C#, if I want to deterministically clean up non-managed resources, I can use the "using" keyword. But for multiple dependent objects, this ends up nesting further and further: using (FileStream fs = new FileStream("c:\file.txt", FileMode.Open)) { using (BufferedStream bs = new BufferedStream(fs)) { using (StreamReader sr = new StreamReader(bs)) { // use sr, and have everything cleaned up when done. } } } In C++, I'm used to being able to use destructors to do it like this: { FileStream fs(

Effective C++ Notes(读书笔记)

限于喜欢 提交于 2019-12-09 12:05:57
1,视C++为一种语言联邦,大致分为4个部分: A)C。说到底C++仍是以C为基础。区块、语句、预处理器、内置数据类型、数组、指针等等统统来自C。 B)Object-Oriented C++。这部分也就是C with Classes所诉求的:classes(包括构造函数和虚构函数)、封装、继承、多态,虚函数等等。 C)Template C++。这是C++的范型编程部分,tamplates威力强大,它给我们带来了崭新的编程范型,也就是所谓的TMP模板元编程。 D)STL。STL是个template程序库,它对容器、迭代器、算法以及函数对象的规范有极佳的紧密配合与协调,然后template及程序库也可以其他想法建置出来。 2,尽量使用const,enum,inline代替#define A)#define不被视为语言的一部分,属于预处理指令,编译不会计入符号表无法调试。 B)#define在预处理器处理阶段只做简单的替换,这将带来很多预期意外的行为。如 #define MAX(a, b) ((a)>(b)?(a):(b)) 尽管上述宏定义已将变量用括号括起来了,但是还是不能避免MAX(++a, b+10)这样给a所带来的两次不是预期内的自增行为。以为替换为: template<typename T> inline T Max(const T& a, const T& b) {

Is relying on the type of a Windows handle being a pointer ok?

偶尔善良 提交于 2019-12-07 13:56:19
问题 Windows handles are sometimes annoying to remember to clean up after (doing GDI with created pens and brushes is a great example). An RAII solution is great, but is it really that great making one full (Rule of Five) RAII class for each different type of handle? Of course not! The best I can see would be one full generic RAII class with other classes just defining what to do when the handle should be cleaned up, as well as other handle-specific aspects. For example, a very simple module class

shared_ptr: what's it used for

半世苍凉 提交于 2019-12-07 11:41:54
问题 I make a lot of use of boost::scoped_ptr in my code and it is great but I'm currently working with software that uses shared_ptr all over the place and I'm wondering if I'm missing something. AFAIK a shared_ptr is only useful if different threads are going to be accessing the same data and you don't know what order the threads are going to finish (with the shared_ptr ensuring that the object exists until the last thread has finished with it). Are there other use cases? 回答1: Threads are

Why can't Alexandrescu use std::uncaught_exception() to implement SCOPE_FAIL in ScopeGuard11? [duplicate]

跟風遠走 提交于 2019-12-06 17:12:23
问题 This question already has answers here : Scope(failure) in C++11? (2 answers) Closed 6 years ago . Many people are no doubt familiar with Mr. Alexandrescus ScopeGuard template (now part of Loki) and the new version ScopeGuard11 presented here: http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C with source here: https://gist.github.com/KindDragon/4650442 In his talk at c++ and beyond 2012 he mentioned that he couldn't find a way to

How to do “try/finally” in C++ when RAII is not possible?

南笙酒味 提交于 2019-12-06 12:56:56
I'm coming back to C++ from a heavy C# background and I've inherited some C++ codebase which I think might not have been in line with the best C++ practices. For example, I'm dealing with the following case (simplified): // resource class Resource { HANDLE _resource = NULL; // copying not allowed Resource(const Resource&); Resource& operator=(const Resource& other); public: Resource(std::string name) { _resource = ::GetResource(name); if (NULL == _resource) throw "Error"; } ~Resource() { if (_resource != NULL) { CloseHandle(_resource); _resource = NULL; }; } operator HANDLE() const { return

c++ new C array allocation, RAII or simple shared_ptr / boost::shared_array

人走茶凉 提交于 2019-12-06 11:19:11
I am learning c++ and i stumbled once again on a new issue. I do need to allocate a C array for a library to use, but in a safe way, ofcourse. I already found that delete[]; at the end of method fails miserably. OLD, not that good: float *buf; try { buf = new float[daswidth*chans_info.chans*sizeof(float)]; } catch (std::bad_alloc& ba) // sometimes it throws ! so i do need to stop my execution. { if (DEBUG) tekstasFormat(L"WARNING: bad alloc caught: %s", ba.what()); return; // skip this iteration then. } //... OUR CODE delete[] buf; So what i tryed to use which works perfectly instead of my old