POV-Ray : Newsgroups : povray.off-topic : Learning C# : Re: Learning C# Server Time
29 Jul 2024 04:23:53 EDT (-0400)
  Re: Learning C#  
From: Warp
Date: 1 Oct 2012 09:29:38
Message: <50699ac2@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> > Since the compiler can track when the object goes out of scope,

> But C++ can't do this

Of course it can. That's the very essence of RAII.

>, which is why you wind up with all kinds of smart 
> pointer types. You can make it work, but you can also get it wrong.

You are confusing tracking the scope of an object with tracking references
that point to a dynamically allocated object. Those are completely different
things.

> If you take a mutex, and pass it to a subroutine by value, you've just 
> screwed your lock

Which is why mutex objects cannot be passed by value (ie. they have their
copy constructors and copy assignment operators disabled).

(If you really need to pass such an object as a function parameter, then
you can do it by using *move* semantics rather than *copy* semantics.)

>, because you'll free the lock when you return from the 
> subroutine, and you'll free the already-disposed lock a second time when you 
> return from the call that passed it.

std::unique_ptr does not reference-count, it contains nothing more than
the pointer to the object (which itself doesn't need to contain anything
at all related to memory management), and you can give std::unique_ptrs
as function parameters (and use them eg. in data containers). You can also
eg. return one as the return value of a function. It it does not delete the
object twice.

(The reason is that it uses move semantics rather than copy semantics.)

> Singularity does it by making you declare the function like this:
> void xyz([claimed] tracked one, tracked two) { destroy one; }
> (or some such syntax)

What exactly stops the compiler from generating the destructor call
automatically? Why exactly do you have to write it explicitly?

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.