c++ - error: use of deleted function - solution? - Stack Overflow

admin2025-04-20  0

In error: use of deleted function an error is explained, but it is not explained how to resolve the error. Consider the following c++ code.

struct foo{
    int& i;
};

int main() {
    int i = 0, j = 1;
    foo v = {i};
    v = {j};
    return 0;
}

This results in error: use of deleted function ‘foo& foo::operator=(foo&&)’ referring to v = {j};. This can be resolved as follows.

struct foo{
    int& i;
};

int main() {
    int i = 0, j = 1;
    foo v = {i};
    foo v2 = {j};
    return 0;
}

But this is ridiculous. I don't want to declare a new variable, I just want to get rid of the old instance of v and assign it a new value. Is there really no way to simply redefine v?

In error: use of deleted function an error is explained, but it is not explained how to resolve the error. Consider the following c++ code.

struct foo{
    int& i;
};

int main() {
    int i = 0, j = 1;
    foo v = {i};
    v = {j};
    return 0;
}

This results in error: use of deleted function ‘foo& foo::operator=(foo&&)’ referring to v = {j};. This can be resolved as follows.

struct foo{
    int& i;
};

int main() {
    int i = 0, j = 1;
    foo v = {i};
    foo v2 = {j};
    return 0;
}

But this is ridiculous. I don't want to declare a new variable, I just want to get rid of the old instance of v and assign it a new value. Is there really no way to simply redefine v?

Share Improve this question asked Mar 2 at 9:51 SmileyCraftSmileyCraft 2891 silver badge11 bronze badges 9
  • The code would initialize the reference i with a reference to a temporary, a disaster. – Peter - Reinstate Monica Commented Mar 2 at 9:58
  • You could make int& i into std::reference_wrapper<int> i. – wohlstad Commented Mar 2 at 10:00
  • @Peter-ReinstateMonica Is this not the case in general when you have a reference field in a struct? If this should be described as a disaster, then why is this even a feature? I suppose one possible fix is to use a pointer instead of a reference, but then I can't really use the const keyword to declare that the struct is not allowed to change the object. – SmileyCraft Commented Mar 2 at 10:01
  • @SmileyCraft My comment was also not correct; the reference to the integer j would stay valid. – Peter - Reinstate Monica Commented Mar 2 at 10:06
  • 1 "I can't really use the const keyword to declare that the struct is not allowed to change the object" - you can have pointer-to-const just like ref-to-const. – Jeff Garrett Commented Mar 2 at 15:31
 |  Show 4 more comments

2 Answers 2

Reset to default 4

Since your struct foo contains a member i which is a reference, it is non-copyable and non-assignable.

In order to make it copyable/assignable, you can use std::reference_wrapper:

std::reference_wrapper is a class template that wraps a reference in a copyable, assignable object.

#include <functional>   // for std::reference_wrapper

struct foo {
    std::reference_wrapper<int> i;
};

int main() {
    int i = 0, j = 1;
    foo v = { i };
    v = { j };
    return 0;
}

Live demo

The simplest and most adequate solution here is to use a member pointer instead of a reference. The two are very similar — the main difference is that pointers can change what they refer to, which is exactly what you are trying to do with the assignment. References, by contrast, are immutable aliases for objects.

If you want to assign objects of foo, reference members simply are the wrong type choice.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745125579a286406.html

最新回复(0)