Alex Rivera | Logout

Is RVO allowed when a copy constructor is private and not implemented?

Asked 2012-04-24T08:25:45.853
12

Suppose I have a class where the copy constructor is private and not implemented (to make the object non-copyable)

class NonCopyable {
// whatever
 private:
    NonCopyable( const NonCopyable&);
    void operator=(const NonCopyable&);
 };

Now in some member function of the same class I write code that returns an object of that class:

NonCopyable NonCopyable::Something()
{
    return NonCopyable();
}

which is a case when RVO could kick in.

RVO still requires that a copy constructor is accessible. Since the possible call to the copy constructor is done from within the same class member function the copy constructor is accessible. So technically RVO is possible despite the fact that the intent was to prohibit using the copy constructor.

Is RVO allowed in such cases?

Edit
Report

1 Answer

0

It is allowed, but you may get a link error because the copy constructor is not implemented. If you provide a body for NonCopyable( const NonCopyable&), it would work.

answered 2012-04-24T08:30:41.993

Your Answer