object-lifetime

Lifetime of lambda objects in relation to function pointer conversion

☆樱花仙子☆ 提交于 2019-11-28 08:01:23
Following this answer I'm now wondering what the rules are for the lifetime of lambdas and how the relate to the lifetime of function pointers which are created by automatic conversion. There are several questions about the lifetime of lambdas (e.g. here and here ), in which case the answers are "they behave exactly like you wrote the full functor object yourself", however neither address the conversion to function pointer which could quite sensibly be a special case. I put together this small working example that illustrates my concern: #include <iostream> typedef int (*func_t)(int); // first

Why is a variable declared in a using statement treated as readonly?

爷,独闯天下 提交于 2019-11-28 07:59:20
问题 Why using variable treated as readonly? It is c# language specification or managed languages specification? It is because c# is a .net language? Thanks in advance. Note : using variable is variable that appears in using statement sample code: using (Form s = new Form) { myfunc(ref s); } we cant change value of a using variable in using block. the code will raise an error. Note : i dont want you discuss about readonly keyword. 回答1: I'm looking at an (outdated?) spec [1] right now. 15.13 says

Are locals destroyed before or after evaluation of a function return value?

一个人想着一个人 提交于 2019-11-28 07:31:37
问题 I am thinking of making a class which represents ownership of a synchronization primitive, something like this: class CCriticalSectionLock { public: CCriticalSectionLock( CCriticalSection &cs ) : cs( cs ) { cs.Enter(); } ~CCriticalSectionLock() { cs.Leave(); } private: CCriticalSection &cs; }; This looks like a good way to be able to take ownership during a function and ensure ownership is released even if there are multiple exit points or exceptions. It does, however, raise some subtle

How do the ISponsor and ILease interfaces work?

五迷三道 提交于 2019-11-28 01:24:00
I've created a object that inherits from MarshalByRefObject and ISponsor . In my implementation of ISponsor I just return a timespan to indicate how long I want the object renewed for. When I call InitializeLifetimeService() to get an ILease reference to be passed into my ISponsor object it never appears to be used from examples I've seen . ISponsor just seems to return a TimeSpan without actually using the ILease reference. But I'm sure there is more going on here since remoting is involved. How do ISponsor and ILease work, specifically in terms of object lifetime renewal? In parent AppDomain

Will a reference bound to a function parameter prolong the lifetime of that temporary?

孤街浪徒 提交于 2019-11-28 00:29:41
问题 I have this code (simplified version): const int& function( const int& param ) { return param; } const int& reference = function( 10 ); //use reference I can't quite decide to which extent C++03 Standard $12.2/5 wording The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference... is applicable here. Is reference variable in the code above valid or dangling? Will the

What is the order of destruction of function parameters?

我只是一个虾纸丫 提交于 2019-11-27 23:13:21
问题 This is a follow-up to my previous question What is the order of destruction of function arguments? because I accidentally confused arguments with parameters. Thanks to Columbo and T.C. for clearing me of terminology confusion in the comments of that question. If the body of some function f with parameters p_1 , ..., p_n of types T_1 , ..., T_n respectively throws an exception, finishes or returns, in what order are the parameters destroyed and why? Please provide a reference to the standard,

Extending temporary's lifetime through rvalue data-member works with aggregate, but not with constructor, why?

荒凉一梦 提交于 2019-11-27 22:21:07
I've found the following scheme to extend a temporaries lifetime works, I don't know if it should, but it does. struct S { std::vector<int>&& vec; }; int main() { S s1{std::vector<int>(5)}; // construct with temporary std::cout << s1.vec[0] << '\n'; // fine, temporary is alive } However, when S is given an explicit value constructor it is no longer an aggregate, and this scheme fails with an invalid read on s1.vec[0] struct S { std::vector<int>&& vec; S(std::vector<int>&& v) : vec{std::move(v)} // bind to the temporary provided { } }; int main() { S s1{std::vector<int>(5)}; // construct with

Invoking virtual method in constructor: difference between Java and C++

流过昼夜 提交于 2019-11-27 19:29:13
In Java: class Base { public Base() { System.out.println("Base::Base()"); virt(); } void virt() { System.out.println("Base::virt()"); } } class Derived extends Base { public Derived() { System.out.println("Derived::Derived()"); virt(); } void virt() { System.out.println("Derived::virt()"); } } public class Main { public static void main(String[] args) { new Derived(); } } This will output Base::Base() Derived::virt() Derived::Derived() Derived::virt() However, in C++ the result is different: Base::Base() Base::virt() // ← Not Derived::virt() Derived::Derived() Derived::virt() (See http://www

Best practice to do nested TRY / FINALLY statement

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 17:53:39
Hi What is the best way to do nested try & finally statements in delphi? var cds1 : TClientDataSet; cds2 : TClientDataSet; cds3 : TClientDataSet; cds4 : TClientDataSet; begin cds1 := TClientDataSet.Create(application ); try cds2 := TClientDataSet.Create(application ); try cds3 := TClientDataSet.Create(application ); try cds4 := TClientDataSet.Create(application ); try /////////////////////////////////////////////////////////////////////// /// DO WHAT NEEDS TO BE DONE /////////////////////////////////////////////////////////////////////// finally cds4.free; end; finally cds3.free; end; finally

AppDomain and MarshalByRefObject life time : how to avoid RemotingException?

不想你离开。 提交于 2019-11-27 17:09:54
When a MarshalByRef object is passed from an AppDomain (1) to another (2), if you wait 6 mins before calling a method on it in the second AppDomain (2) you will get a RemotingException : System.Runtime.Remoting.RemotingException: Object [...] has been disconnected or does not exist at the server. Some documentation about this isse : http://blogs.microsoft.co.il/blogs/sasha/archive/2008/07/19/appdomains-and-remoting-life-time-service.aspx http://blogs.msdn.com/cbrumme/archive/2003/06/01/51466.aspx - Instance Lifetime, cbrumme says "We should fix this." :( Correct me if I'm wrong : if