On 2015-08-03 22:51, Doug Mika wrote:
> On Saturday, August 1, 2015 at 2:22:34 PM UTC-5, Bo Persson wrote:
>> On 2015-08-01 20:43, Doug Mika wrote:
>>> Quick question:
>>>
>>> explicit variable decleration (I explicitly declare variable dummyLck):
>>> unique_lock<std::mutex> dummyLck(dummyMtx); //of course this works
>>>
>>> but I want to declare the dummyLck as an "implicit" variable without a name, and pass it directly to a condition variable, but the following line spits syntax errors...what's the syntax for doing this:
>>> condition_variable threadsReady;
>>> mutex dummyMtx;
>>> threadsReady.wait(unique_lock<std::mutex>(dummyMtx)); //this syntax seems to be wrong, but why?
>>>
>>
>> It's not the syntax, it's the semantics. The wait function takes a
>> non-const reference to a lock. That will not bind to a temporary.
>>
>>
>>
>> Bo Persson
>
> So the question that comes to me now is how did you know that the wait function takes a non-const reference to a lock?
I guessed that from your problem, and from knowing that a
condition_variable must do something to the mutex. Like unlock it while
waiting.
Then I checked the reference. :-)
> And how do you know that these don't allow rvalues?
I'm an old guy, and remember reading that Bjarne initially allowed
references to tempories but found that it caused too many bugs. Updates
to temporaries were "mysteriously" lost.
For example:
void f(long& x)
{ ++x; }
int y = 0;
f(y); // not allowed
If this was allowed, the int would be converted to a temporary long, and
the long would incremented, not the int.
By limiting temporaries to const references, they cannot be changed (as
they are const) and this kind of bugs goes away.
> I often use
www.cplusplus.com as reference for all these little tidbits on various functions, and nowhere did I find what you wrote.
>
Unfortunately, the C++ standard has to be read holistically. It often
says "unless otherwise specified", and then you have to read everything
else to see if it IS specified differently 200 pages further on.
> How can I deduce when I can use implicit variables as function parameters, and when I cannot?
>
If they are passed by value, or by const reference, or by rvalue
reference... Just not by non-const lvalue reference. :-)
Bo Persson