To answer other questions. The following is a comma operator call. It creates a temporary MyClass that includes a call to its constructor. Then it evaluates the second expression cout << "Y" << endl , which will print Y. Then, at the end of the full expression, it destroys the temporary one that its destructor will call. Therefore, your expectations were correct.
MyClass (12345), cout << "Y" << endl;
To do the following, you must add parentheses because the comma has a predefined value in the declarations. It will start declaring the some_func function, returning an int and not taking any parameters, and assigns a scoped_lock x object. Using parentheses, you say that all this is one expression of a comma operator.
int x = (boost::scoped_lock (my_mutex), some_func());
It should be noted that the following two lines are equivalent. The first does not create a temporary unnamed object using my_mutex as the constructor argument, but instead the parentheses around the name are redundant. Do not let the syntax confuse you.
boost::scoped_lock(my_mutex); boost::scoped_lock my_mutex;
I have seen the misuse of the terms scope and lifetime.
Scope is a place where you can refer to a name without specifying its name. Names have scopes, and objects inherit the scope of the name used to define them (thus, sometimes the standard says "local object"). The temporary object does not have scope because it did not receive a name. Similarly, an object created by new does not have scope. Scope is a compile-time property. This term is often used incorrectly in the standard; see this defect report , so itβs quite difficult to find the real meaning.
Lifetime is a runtime property. This means that the object is configured and ready to use. For an object of type class, the lifetime begins when the constructor completes execution, and ends when the destructor begins execution. Life time is often confused with volume, although these two things are completely different.
The exact lifetime of the time series is determined. Most of them end their life after evaluating the full expression in which they are contained (for example, the comma operator above or the assignment expression). Time series can be associated with constant links that extend their lifespan. Objects thrown by exceptions are also temporary, and their lifespan ends when they no longer have a handler.
Johannes Schaub - litb Sep 07 '09 at 15:54 2009-09-07 15:54
source share