object-lifetime

Destructor call in a comma-separated expression

早过忘川 提交于 2019-12-21 10:16:34
问题 consider the following example program: #include <iostream> using namespace std; struct t { ~t() {cout << "destroyed\n"; } }; int main() { cout << "test\n"; t(), cout << "doing stuff\n"; cout << "end\n"; } The output I get with GCC 4.9.2 is: test doing stuff destroyed end cpp.sh link: http://cpp.sh/3cvm However according to cppreference about the comma operator: In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before

Destructor call in a comma-separated expression

十年热恋 提交于 2019-12-21 10:16:18
问题 consider the following example program: #include <iostream> using namespace std; struct t { ~t() {cout << "destroyed\n"; } }; int main() { cout << "test\n"; t(), cout << "doing stuff\n"; cout << "end\n"; } The output I get with GCC 4.9.2 is: test doing stuff destroyed end cpp.sh link: http://cpp.sh/3cvm However according to cppreference about the comma operator: In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before

Reference to an unnamed temporary object (life time)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 08:56:16
问题 After reading this answer from ildjarn, I wrote the following example, and it looks like an unnamed temporary object has the same life time as its reference! How come this is possible? Is it specified in the C++ standard? Which version? Source code: #include <iostream> //cout #include <sstream> //ostringstream int main () { std::ostringstream oss; oss << 1234; std::string const& str = oss.str(); char const* ptr = str.c_str(); // Change the stream content oss << "_more_stuff_"; oss.str(""); /

Is taking the address of a member of an uninitialized object well defined?

心已入冬 提交于 2019-12-19 08:14:07
问题 Consider the following example. When bar is constructed, it gives it's base type ( foo ) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struct foo { foo(int * p_x) : x(p_x) {} int * x; }; struct member { member(int p_y) : y(p_y) {} int y; }; struct bar : foo { bar() : foo(&my_member.y), my_member(42) {} member my_member; }; #include <iostream> int main() { bar my_bar; std::cout << *my_bar.x; } Is this well defined? Is it legal to take

Is taking the address of a member of an uninitialized object well defined?

南笙酒味 提交于 2019-12-19 08:13:04
问题 Consider the following example. When bar is constructed, it gives it's base type ( foo ) constructor the address of my_member.y where my_member is data member that hasn't been initialized yet. struct foo { foo(int * p_x) : x(p_x) {} int * x; }; struct member { member(int p_y) : y(p_y) {} int y; }; struct bar : foo { bar() : foo(&my_member.y), my_member(42) {} member my_member; }; #include <iostream> int main() { bar my_bar; std::cout << *my_bar.x; } Is this well defined? Is it legal to take

Does destroying and recreating an object make all pointers to this object invalid?

南笙酒味 提交于 2019-12-19 05:57:07
问题 This is a follow-up to this question. Suppose I have this code: class Class { public virtual method() { this->~Class(); new( this ) Class(); } }; Class* object = new Class(); object->method(); delete object; which is a simplified version of what this answer suggests. Now once a destructor is invoked from within method() the object lifetime ends and the pointer variable object in the calling code becomes invalid. Then the new object gets created at the same location. Does this make the pointer

Why does the variable not live long enough?

冷暖自知 提交于 2019-12-19 05:47:29
问题 Consider this function that should return the file extension of a given Path . pub fn get_extension<'a>(path: &'a Path) -> Option<&'a str> { let path_str = path.as_str().unwrap(); let ext_pos = regex!(".[a-z0-9]+$").find(path_str); match ext_pos { Some((start, _)) => { return Some(path_str.as_slice().slice_from(start)) }, None => return None } } The error message is as follows: `path_str` does not live long enough The error message is pretty clear and it's a shame I can't work it out on my

Is it wrong to use braces for variable scope purposes?

允我心安 提交于 2019-12-19 05:08:05
问题 I sometimes use braces to isolate a block of code to avoid using by mistake a variable later. For example, when I put several SqlCommand s in the same method, I frequently copy-paste blocks of code, ending by mixing the names and executing twice some commands. Adding braces helps to avoid this situation, because using a wrong SqlCommand in a wrong place will result in an error. Here's an illustration: Collection<string> existingCategories = new Collection<string>(); // Here a beginning of a

Struct that owns some data and a reference to the data [duplicate]

淺唱寂寞╮ 提交于 2019-12-19 02:20:22
问题 This question already has an answer here : How to initialize struct fields which reference each other (1 answer) Closed 4 years ago . Construction of an object allocates data needed for lifetime of that object, but also creates another object that needs to keep references to the data: pub fn new() -> Obj { let data = compute(); Obj { original: data, processed: AnotherObj { reference: &data } } } Is it possible to express this in Rust's terms? Here I'd like Obj , AnotherObj and data to have

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

佐手、 提交于 2019-12-18 18:09:18
问题 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