Is there a valid case for creating a temporary that is immediately destroyed and is not used directly in C++?

Viewed 334

Inspired by this question. Suppose I have class Lock with a default constructor and in some code I write the following statement:

Lock();

this will have the effect of creating a temporary object of class Lock and immediately destroying it. Of course, creation could have some side effects and that would alter the program behavior, however that looks rather weird.

So my first guess is that such statements while completely valid from the language perspective are highly likely to contain a logical error.

Are there some valid usecases of the above statement? Are there some well-known and popular idioms that include such statements? Why would I want such statements in a correct program?

5 Answers
Related