3

I have the following class:

class BluetoothAddress
{
public:
    BluetoothAddress();
    BluetoothAddress(const unsigned char* address);
    BluetoothAddress(const BluetoothAddress& bluetoothAddress);
    ~BluetoothAddress();

    BluetoothAddress& operator=(const BluetoothAddress& other);

    BluetoothAddress(BluetoothAddress&& other) = delete;
    BluetoothAddress& operator=(BluetoothAddress&& other) = delete;

    unsigned char address[BLUETOOTH_ADDRESS_LENGTH];
};

And the following function:

BluetoothAddress Create()
{
    unsigned char addr = { 0x12, 0x34, 0x56, 0x78 0x9A, 0xBC };
    return BluetoothAddress(addr);
}

I received this when I try to compile:

Error C2280 'BluetoothAddress::BluetoothAddress(BluetoothAddress &&)': attempting to reference a deleted function

I do not need a move constructor since there aren't any resources on the heap which need to be moved. (The constructor receiving the unsigned int* is just copying the values in a loop char by char).

Why is the compiler trying to call the move constructor instead of the copy constructor?

UnTraDe
  • 3,747
  • 10
  • 36
  • 60
  • While I agree that this appears to be an issue (compiler complaining and insisting on using a move operator when a copy operator is available)... I don't see why it would hurt to provide a move operator anyway. – inetknght Nov 02 '15 at 16:43
  • Also "just copying the address" is a bug, especially in this case because that address is local to a function that has returned. – Adam Nov 02 '15 at 16:44
  • 1
    The relevant line in http://stackoverflow.com/questions/16703798/why-is-my-object-not-being-copied-when-move-constructor-is-deleted is "When you do [!] have a move constructor, even it is marked deleted, the rvalue from the return statement will find the move constructor a better match than the copy constructor." –  Nov 02 '15 at 17:11
  • If you really don't want a move constructor, then *don't write one*. Since your class has a user-declared destructor, there will be no implicitly-generated move constructor; so a copy/move operation will use the copy constructor. (However this is a bad idea in general, it makes your code slower for no reason other than lack of understanding of move semantics). – M.M Nov 02 '15 at 19:18

0 Answers0