-4

Let's say I define the following operator:

const int & operator= (const MyClass &);

Can I use it to make an assignment to a non-const variable?

Reading through the comments here, my understanding is that yes, I can (i.e. I could do things like a=b even if a is not const and the compiler wouldn't complailn).

However, when I try the following code:

int main()
{
  int x = 42;
  const int &y = x;  // y is a const reference to x
  int &z = y;  
}

It fails with the following:

compilation
execution
main.cpp:9:8: error: binding reference of type 'int' to value of type 'const int' drops 'const' qualifier
  int &z = y;
       ^   ~
1 error generated.
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
  • 1
    But `int z = y;` will work, which is what the other question was about. – Richard Critten Dec 31 '22 at 17:59
  • 3
    `int &z = y`; isn't assignment, it's a form of initialization, despite the `=` sign. For classes it would call a constructor, not `operator=`. An assignment would be `z = y;` on a separate line. But to test your `operator=` like this, you'd have to use an actual instance of the class you overloaded `=` for. – HolyBlackCat Dec 31 '22 at 18:00
  • You can always copy a value, even if you use a reference to a constant value. The copy doesn't need to be constant. – Some programmer dude Dec 31 '22 at 18:01
  • Sure, you can always cast away constness, but then you'd be violating the user contract. Declaring something `const` means you promise not to try to modify it. You need a _really_ good reason to make your code lie about that. – Dúthomhas Dec 31 '22 at 18:05
  • 1
    @HolyBlackCat Are you sure that a constructor will be called if you initialize a reference? – gerum Dec 31 '22 at 18:08
  • 1
    Here's a [demonstration](https://godbolt.org/z/GMfhs7YnG) of code that actually uses your proposed `operator=`. `first = second` performs an assignment to the non-`const` `first`. The result can be assigned to the non-`const` `x`. I'm not sure what, if anything, your "the following code" has to do with the `operator=` you've written. – Nathan Pierson Dec 31 '22 at 18:22
  • @gerum I meant `MyClass x = ...;`, probably wasn't the best wording. – HolyBlackCat Dec 31 '22 at 18:29

1 Answers1

1

Yes, you can assign a const reference to a non-reference variable.

 int x = 42;
 const int &y = x;  // y is a const reference to x
 int w = y; // this works
 int z& = y; // this doesn't  

You can assign a const reference to a non-reference because the value is copied, meaning that x won't be modified if w is modified as w is a copy of the data.

You cannot assign a const reference to a non-const reference as when you are using a const reference you are giving a guarantee that you won't modify the pointed value. If you would be allowed to assign a const reference to a non-const reference you would break this guarantee.

Cedric Martens
  • 1,139
  • 10
  • 23
  • You totally _could_ do it, tho. (It would still be immoral.) – Dúthomhas Dec 31 '22 at 18:17
  • @Dúthomhas It's also opening the door for undefined behaviour (for example, if the example was modified so `x` was `const`, you use some trick to force creation of `z` as a non-`const` reference to `x`, so `x` could subsequently be modified via an assignment to `z`). The fact that you *can* do something (in this case, bludgeon a compiler into submission, so it stops diagnosing something as an error) doesn't mean it is advisable or desirable. – Peter Dec 31 '22 at 18:29
  • @Peter Or the user thinks it is safe to alias some object in memory with access restrictions on it, or the target object doesn't actually exist as its own distinct entity anywhere... – Dúthomhas Dec 31 '22 at 18:47