News for hackers. Stuff that matters.
Re:Puzzle Languages by zabzonk on Wednesday October 04, 2023 @06:38AM (#37763229)
i would like to see an example of how the c++ compiler destructs an object before you use it. the only reason for this to happen that i can imagine is not understanding the concept of scope.
Re:Puzzle Languages by keithalewis on Wednesday October 04, 2023 @06:41AM (#37763259)
{ std::string s; }
Re:Puzzle Languages by zabzonk on Wednesday October 04, 2023 @06:56AM (#37763373)
see "scope" in my original comment
Re:Puzzle Languages by foldr on Wednesday October 04, 2023 @06:46AM (#37763288)
This could happen any time you return a pointer to a variable with automatic storage and then later attempt to dereference the pointer and do something with the value. Of course, you shouldn't do that, but the C++ compiler won't stop you. 'Scope' can have various meanings, but merely understanding how lexical scoping of variables works is not sufficient to understand why you shouldn't do that.
Re:Puzzle Languages by zabzonk on Wednesday October 04, 2023 @07:03AM (#37763427)
well, let me rephrase "scope" as "lifetime of objects"
Re:Puzzle Languages by foldr on Wednesday October 04, 2023 @07:17AM (#37763541)
Ok, but then what you're saying is almost tautological. Indeed, you won't be surprised when an object is destroyed in C++ if you understand how object lifetimes work in C++.
Re:Puzzle Languages by bradrn on Wednesday October 04, 2023 @06:49AM (#37763315)
As I recall, the issue in my case involved a default copy-constructor which destructed an object wrongly. I’ve since learnt to delete the implicit methods of a class, but I still find reasoning about copy and move semantics to be thoroughly puzzling.
Re:Puzzle Languages by zabzonk on Wednesday October 04, 2023 @06:59AM (#37763402)
i would still think this is a misunderstanding of scope. and you really don't want to be worrying about move semantics unless you are a performance maniac, a library writer, or (most likely) both.