Consequences of deleting move ctor

A

Alfredo Vernucci

Hello,

I am experimenting with C++0x and I have some problems with move operations.

I have a class that shall not be moved and therefore I have deleted from it both the move constructor and the move assignment operator. But then I have discovered that I cannot return instances of that class by value.

With the example reported here below, gcc 4.6 provides the following error message: error: use of deleted function 'A::A(A&&)' (btw, I get a similar error message also with MSVC2010).

class A
{
public:
A() {}
A(const A&) {}
A(A&&) = delete;

auto operator=(A&&) -> A& = delete;

auto foo() -> A
{
return A();
}
};

In this example, since the move constructor is not available, I would have expected the copy constructor to be called when foo() returns. But clearly this is not the case.

Question: what should I do if I want to forbid move operations for a class and yet be able to return instances of that class by value?

Thanks in advance,
Alfredo
 
M

Marc

Alfredo said:
I have a class that shall not be moved and therefore I have deleted
from it both the move constructor and the move assignment operator.
But then I have discovered that I cannot return instances of that
class by value.

deleting the function is similar to making it private. If you define a
copy constructor, no move constructor will be generated, no need to
explicitly forbid it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top