object-lifetime

Reusing data member storage via placement new during enclosing object's lifetime

三世轮回 提交于 2019-12-18 18:09:12
问题 This bounty has ended . Answers to this question are eligible for a +100 reputation bounty. Bounty grace period ends in 15 hours . walnut is looking for an answer from a reputable source . This is a follow-up to my previous question where I seem to have made the problem more involved than I had originally intended. (See discussions in question and answer comments there.) This question is a slight modification of the original question removing the issue of special rules during construction

Existence of objects created in C functions

…衆ロ難τιáo~ 提交于 2019-12-18 03:56:26
问题 It has been established (see below) placement new is required to create objects int* p = (int*)malloc(sizeof(int)); *p = 42; // illegal, there isn't an int Yet that is a pretty standard way of creating objects in C. The question is, does the int exist if it is created in C, and returned to C++? In other words, is the following guaranteed to be legal? Assume int is the same for C and C++. foo.h #ifdef __cplusplus extern "C" { #endif int* foo(void); #ifdef __cplusplus } #endif foo.c #include

Is this object-lifetime-extending-closure a C# compiler bug?

ε祈祈猫儿з 提交于 2019-12-17 21:38:46
问题 I was answering a question about the possibility of closures (legitimately) extending object-lifetimes when I ran into some extremely curious code-gen on the part of the C# compiler (4.0 if that matters). The shortest repro I can find is the following: Create a lambda that captures a local while calling a static method of the containing type. Assign the generated delegate-reference to an instance field of the containing object. Result: The compiler creates a closure-object that references the

memcpy/memmove to a union member, does this set the 'active' member?

冷暖自知 提交于 2019-12-17 18:08:07
问题 Important clarification: some commenters seem to think that I am copying from a union. Look carefully at the memcpy , it copies from the address of a plain old uint32_t , which is not contained within a union. Also, I am copying (via memcpy ) to a specific member of a union ( u.a16 or &u.x_in_a_union , not directly to the entire union itself ( &u ) C++ is quite strict about unions - you should read from a member only if that was the last member that was written to: 9.5 Unions [class.union] [

Why can't I dynamic_cast “sideways” during multiple inheritence?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 17:10:08
问题 The following code throws std::bad_cast struct Foo { void foo () {} }; struct Bar { Bar () { dynamic_cast <Foo &> (*this) .foo (); } virtual ~ Bar () {} }; struct Baz : public Foo, public Bar { }; int main () { Baz b; } I remember once reading how dynamic_cast has implementation performance trade-offs because "it traverses the full inheritence lattice" in order to evaluate correctly. What the compiler needs to do here is first cast up and then down again. Is it possible to make the above work

How do the ISponsor and ILease interfaces work?

左心房为你撑大大i 提交于 2019-12-17 16:49:39
问题 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.

Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable?

和自甴很熟 提交于 2019-12-17 02:12:15
问题 From http://en.cppreference.com/w/cpp/string/byte/memcpy: If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined. At my work, we have used std::memcpy for a long time to bitwise swap objects that are not TriviallyCopyable using: void swapMemory(Entity* ePtr1, Entity* ePtr2) { static const int size = sizeof(Entity); char swapBuffer[size]; memcpy(swapBuffer, ePtr1, size); memcpy(ePtr1, ePtr2, size); memcpy(ePtr2, swapBuffer, size); } and

c++ Object parameters: polymorphism, value semantics, object lifetimes?

爱⌒轻易说出口 提交于 2019-12-12 15:15:49
问题 As I make the transition from C# to C++ I get a lot of recommendations to use value semantics where possible. It's pretty much guaranteed that if I post a question with a pointer anywhere someone will come along and suggest that it should be a value instead. I'm starting to see the light and I have found a lot of places in my code where I could replace dynamic allocation and pointers with stack allocated variables (and usually references). So I think I have a grasp on using stack allocated

Extending temporary's lifetime, works with block-scoped aggregate, but not through `new`; why?

青春壹個敷衍的年華 提交于 2019-12-12 08:26:03
问题 Note : This question was originally asked as a comment by Ryan Haining on this answer. struct A { std::string const& ref; }; // (1) A a { "hello world" }; // temporary's lifetime is extended to that of `a` std::cout << a.ref << std::endl; // safe // (2) A * ptr = new A { "hello world" }; // lifetime of temporary not extended? std::cout << ptr->ref << std::endl; // UB: dangling reference Question Why is the lifetime of the temporary extended in (1) , but not in (2) ? 回答1: LONG STORY, SHORT The

C++ Using a reference to the variable being defined

筅森魡賤 提交于 2019-12-12 08:06:14
问题 Is the following code valid C++, according to the standard (discounting the ...s)? bool f(T& r) { if(...) { r = ...; return true; } return false; } T x = (f(x) ? x : T()); It is known to compile in the GCC versions this project uses (4.1.2 and 3.2.3... don't even get me started...), but should it? Edit : I added some details, for example as to how f() conceptually looks like in the original code. Basically, it's meant to be initialize x in certain conditions. 回答1: Syntactically it is, however