which is better: a lying copy constructor or a non-standard one?
<p>I have a C++ class that contains a non-copyable handle. The class, however, must have a copy constructor. So, I've implemented one that transfers ownership of the handle to the new object (as below)</p>

<pre><code>class Foo
{
public:
    Foo() : h_( INVALID_HANDLE_VALUE )
    {
    };

    // transfer the handle to the new instance
    Foo( const Foo&amp; other ) : h_( other.Detach() )
    {
    };

    ~Foo()
    {
        if( INVALID_HANDLE_VALUE != h_ )
            CloseHandle( h_ );
    };

    // other interesting functions...

private:

    /// disallow assignment
    const Foo&amp; operator=( const Foo&amp; );

    HANDLE Detach() const
    {
        HANDLE h = h_;
        h_ = INVALID_HANDLE_VALUE;
        return h;
    };

    /// a non-copyable handle
    mutable HANDLE h_;
}; // class Foo
</code></pre>

<p>My problem is that the standard copy constructor takes a const-reference and I'm modifying that reference. So, I'd like to know which is better (and why):</p>

<ol>
<li><p>a non-standard copy constructor:
<code>Foo( Foo&amp; other );</code></p></li>
<li><p>a copy-constructor that 'lies':
<code>Foo( const Foo&amp; other );</code></p></li>
</ol>

<p>Thanks,
PaulH</p>

<hr>

<p>Edit:</p>

<p><code>DuplicateHandle()</code> only works on specific types of handles. This is not one of them. This handle cannot be duplicated, copied, or cloned. </p>

<p>Several people have pointed out that I was mistaken in suggesting it is a non-standard copy constructor and that <code>std::auto_ptr</code> does this. I think that's probably the way to go. But, I end up with warnings every time I use the class when I make the copy ctor take a non-const value. For example:</p>

<pre><code>namespace detail {
    class Foo { ... };
};

class Buzz
{
public:
    typedef detail::Foo Fuzz;

    Fuzz bar() const { return Fuzz(); }; // warning here
};

warning C4239: nonstandard extension used : 'argument' : conversion from 'Foo' to 'Foo &amp;'
1&gt; A non-const reference may only be bound to an lvalue; copy constructor takes a reference to non-const
</code></pre>

<p>Can anybody suggest what I should do about them?</p>

<p>Thanks,
PaulH</p>

<hr>

<p>Edit2:</p>

<p>Everybody seems to be steering me towards std::auto_ptr&lt;>'s method of doing things. So, I looked there and it uses an intermediate structure to get around the issue I described in the first edit. This is the solution I came up with.</p>

<pre><code>class Foo;

struct Foo_ref
{
    explicit Foo_ref( Foo&amp; other ) : ref_( other ) {};
    Foo&amp; ref_;
private:
    const Foo_ref&amp; operator=( const Foo_ref&amp; );
}; // struct Foo_ref

class Foo
{
public:
    Foo() : h_( INVALID_HANDLE_VALUE )
    {
    };

    // transfer the handle to the new instance
    Foo( Foo_ref other ) : h_( other.ref_.Detach() )
    {
    };

    ~Foo()
    {
        if( INVALID_HANDLE_VALUE != h_ )
            CloseHandle( h_ );
    };

    operator Foo_ref()
    {
        Foo_ref tmp( *this );
        return tmp;
    };

    // other interesting functions...

private:

    /// disallow assignment
    const Foo&amp; operator=( const Foo&amp; );

    HANDLE Detach()
    {
        HANDLE h = h_;
        h_ = INVALID_HANDLE_VALUE;
        return h;
    };

    /// a non-copyable handle
    HANDLE h_;
}; // class Foo
</code></pre>

<p>It compiles cleanly on warning level 4 and seems to work. Please, let me know if it is somehow more irresponsible than my original post.</p>

<p>Thanks,
PaulH</p>
