destructor

Is a copy-on-return operation executed prior or after lock_guard destructor? [duplicate]

 ̄綄美尐妖づ 提交于 2020-07-15 04:36:48
问题 This question already has answers here : C++ return value created before or after auto var destruction? (2 answers) in C++ which happens first, the copy of a return object or local object's destructors? [duplicate] (4 answers) Closed 2 years ago . Is the get_a() function safe for race-conditions or do I need to explicitly copy str_ as in get_b() in order to have a thread-safe function? class Class { public: auto get_a() -> std::string { auto&& guard = std::lock_guard{mutex_}; return str_; }

Destructor of union member seems to be called automatically

三世轮回 提交于 2020-06-27 15:37:08
问题 I'm attempting to implement a tagged union. My understanding was that in a C++ union, the non-trivial (i.e. not empty) destructors of non-static members are never called , thus we have to call them ourselves. That's what I did: #include <iostream> class C { public: C() { std::cout << "C Ctor" << std::endl; } ~C() { std::cout << "C Dtor" << std::endl; } }; class B { public: B() { std::cout << "B Ctor" << std::endl; } ~B() { std::cout << "B Dtor" << std::endl; } }; struct S { int type; union U

As the delete operator deallocates memory, why do I need a destructor?

こ雲淡風輕ζ 提交于 2020-05-14 18:18:05
问题 From c++ FAQ: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9 Remember: delete p does two things: it calls the destructor and it deallocates the memory. If delete deallocates the memory, then what's the need of the destructor here? 回答1: You need to call the destructor in case there are other things that need to be done other than just de-allocating memory. Other than very simple classes, there usually are. Things like closing file handles or shutting down database connections,

Fortran FINAL procedure for ABSTRACT type

佐手、 提交于 2020-02-24 17:01:13
问题 Can I add a final procedure to an abstract type? Suppose the final procedure looks like this: subroutine finalize(this) type(bin_tree_t), intent(inout) :: this deallocate(this%head) end subroutine finalize My compiler (ifort 18.0.1) gives "error #8313: The TYPE(derived-type-spec) shall not specify an abstract type". I get this, but the dummy argument of a final subroutine cannot be polymorphic. If this is not possible, is it then likely to have been a conscious choice of the standards

C++: Concurrency and destructors

前提是你 提交于 2020-02-02 04:49:57
问题 Suppose you have an object which can be accesed by many threads. A critical section is used to protect the sensitive areas. But what about the destructor? Even if I enter a critical section as soon as I enter the destructor, once the destructor has been called, is the object already invalidated? My train of thought: Say I enter the destructor, and I have to wait on the critical section because some other thread is still using it. Once he is done, I can finish destroying the object. Does this

golang-style “defer” in C++ [duplicate]

落爺英雄遲暮 提交于 2020-02-01 01:38:46
问题 This question already has answers here : What is standard defer/finalizer implementation in C++? (8 answers) Closed 4 years ago . I was reading about the go language's defer statement. It allows you to specify an action to take when a function has ended. For example, if you have a file pointer or resource, instead of writing free/delete with every possible return path, you just need to specify the defer function once. It looks like an analogue might be coming to C++ eventually (What is

What if the virtual destructor defined in derived class, but not the top of hierarchy? C++

戏子无情 提交于 2020-01-30 12:25:12
问题 I wonder is it correct to define a base class with no virtual destructor, and define inherited classes with the virtual one? What would actually happen if I do that? 回答1: If you delete p where p is of type X* but actually points to a Y which is derived from X , you have undefined behavior unless X has a virtual destructor. If Y 's destructor is virtual but X s destructor is not it changes exactly nothing. 回答2: If you intend to do delete pB; where pB is of type Base* and pB might point to an