object-lifetime

What is the lifetime and validity of C++ iterators?

核能气质少年 提交于 2019-11-27 14:58:27
问题 I'm planning to implement a list of Things in C++ where elements might be removed out of order. I don't expect that i'll need any kind of random access (i just need to sweep the list periodically), and the order of items isn't important either. So I thought of std::list<Thing*> with this->position = insert(lst.end(), thing) should do the trick. I'd like the Thing class to remember the position of each instance so that i can later easily do lst.erase(this->position) in constant time. However,

Spurious warning about binding temporary to reference member in constructor

筅森魡賤 提交于 2019-11-27 14:37:10
I understand that if a temporary is bound to a reference member in the constructor's initializer list, the object will be destroyed as the constructor returns. However , consider the following code: #include <functional> #include <iostream> using callback_func = std::function<int(void)>; int func(const callback_func& callback) { struct wrapper { const callback_func& w_cb; wrapper(const callback_func& cb) : w_cb {cb} { } int call() { return this->w_cb() + this->w_cb(); } }; wrapper wrp {callback}; return wrp.call(); } int main() { std::cout << func([](){ return 21; }) << std::endl; return 0; }

Lifetime of object is over before destructor is called?

橙三吉。 提交于 2019-11-27 14:25:35
I don't understand this: 3.8/1 "The lifetime of an object of type T ends when: — if T is a class type with a non-trivial destructor (12.4), the destructor call starts , or — the storage which the object occupies is reused or released." If the lifetime ends before the destructor starts, doesn't that mean accessing members in the destructor is undefined behavior? I saw this quote too: 12.7 "For an object with a non-trivial destructor, referring to any non-static member or base class of the object after the destructor finishes execution results in undefined behavior." But it doesn't make clear

C# Thread object lifetime

泪湿孤枕 提交于 2019-11-27 14:02:19
Suppose I have a code as follows: int Main() { if (true) { new Thread(()=> { doSomeLengthyOperation(); }).Start(); } while (true) { //do nothing } } There are 2 threads, I'm going to call the Main thread the thread that is executing the Main() function, and the thread being new'ed up inside the "if" test as Thread A. My question is, when does Thread A get destroyed? Will doSomeLenghtyOperation() be able to run into completion? Since there are no references pointing at Thread A, will it be marked as a candidate for garbage collection: Immediately after the "new Thread().Start()" statement

What is the order of destruction of function arguments?

扶醉桌前 提交于 2019-11-27 13:28:15
问题 If some function f with parameters p_1 , ..., p_n of types T_1 , ..., T_n respectively is called with arguments a_1 , ..., a_n and its body throws an exception, finishes or returns, in what order are the arguments destroyed and why? Please provide a reference to the standard, if possible. EDIT: I actually wanted to ask about function "parameters", but as T.C. and Columbo managed to clear my confusion, I'm leaving this question be about the arguments and asked a new separate question about the

After an object is destroyed, what happens to subobjects of scalar type?

谁说我不能喝 提交于 2019-11-27 08:07:37
问题 Consider this code (for different values of renew and cleanse ): struct T { int mem; T() { } ~T() { mem = 42; } }; // identity functions, // but breaks any connexion between input and output int &cleanse_ref(int &r) { int *volatile pv = &r; // could also use cin/cout here return *pv; } void foo () { T t; int &ref = t.mem; int &ref2 = cleanse ? cleanse_ref(ref) : ref; t.~T(); if (renew) new (&t) T; assert(ref2 == 42); exit(0); } Is the assert guaranteed to pass? I understand that this style is

__del__ method being called in python when it is not expected

大城市里の小女人 提交于 2019-11-27 07:57:14
问题 I am new to python and have been working through the examples in Swaroop CH's "A Byte of Python". I am seeing some behavior with the __del__ method that is puzzling me. Basically, if I run the following script (in Python 2.6.2) class Person4: '''Represents a person''' population = 0 def __init__(self, name): '''Initialize the person's data''' self.name = name print 'Initializing %s'% self.name #When the person is created they increase the population Person4.population += 1 def __del__(self):

Is circumventing a class' constructor legal or does it result in undefined behaviour?

六月ゝ 毕业季﹏ 提交于 2019-11-27 06:39:29
问题 Consider following sample code: class C { public: int* x; }; void f() { C* c = static_cast<C*>(malloc(sizeof(C))); c->x = nullptr; // <-- here } If I had to live with the uninitialized memory for any reason (of course, if possible, I'd call new C() instead), I still could call the placement constructor. But if I omit this, as above, and initialize every member variable manually, does it result in undefined behaviour? I.e. is circumventing the constructor per se undefined behaviour or is it

Why can a non-const reference parameter be bound to a temporary object?

可紊 提交于 2019-11-27 03:39:33
问题 char f1(); void f2(char&); struct A {}; A f3(); void f4(A&); int main() { f2(f1()); // error C2664. This is as expected. f4(f3()); // OK! Why??? } error C2664: 'void f4(char &)' : cannot convert argument 1 from 'char' to 'char &' I have been taught that in C++ a non-const reference parameter cannot be bound to a temporary object; and in the code above, f2(f1()); triggers an error as expected. However, why does the same rule not apply to the code line f4(f3()); ? PS: My compiler is VC++ 2013.

Why does calling std::string.c_str() on a function that returns a string not work?

六眼飞鱼酱① 提交于 2019-11-27 03:22:06
问题 I have the following code: std::string getString() { std::string str("hello"); return str; } int main() { const char* cStr = getString().c_str(); std::cout << cStr << std::endl; // this prints garbage } What I thought would happen is that getString() would return a copy of str ( getString() returns by value); thus, the copy of str would stay "alive" in main() until main() returns. This would make cStr point to a valid memory location: the underlying char[] or char* (or whatever) of the copy