------- Comment #5 from jwakely dot gcc at gmail dot com 2009-12-11 10:39 ------- (In reply to comment #4) > > But I'm not convinced that doing this is always a mistake. > > Since we don't care about the object, we must care about the constructor side > effect. I seem to be under the impression that ISO C++ allows the construction > of temporary objects to be optimized away---even if there are side effects in > the constructor or destructor! Therefore, it's hard to see a valid use case > for > this.
Certain temporaries (such as those created during copying or reference binding) can be optimised away, I don't think it's true of temporaries created explicitly like this. e.g. I think this should work: std::ofstream("lockfile"); // creates ./lockfile if it doesn't exist (assuming write permission in the directory, and ignoring races and other reasons it might be a bad idea) -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36587