assignment-operator

Why are there no ||= or &&= operators in C#?

懵懂的女人 提交于 2019-11-27 23:24:39
We have equivalent assignment operators for all Logical operators, Shift operators, Additive operators and all Multiplicative operators. Why did the logical operators get left out? Is there a good technical reason why it is hard? Why did the logical operators get left out? Is there a good technical reason why it is hard? They didn't . You can do &= or |= or ^= if you want. bool b1 = false; bool b2 = true; b1 |= b2; // means b1 = b1 | b2 The || and && operators do not have a compound form because frankly, they're a bit silly. Under what circumstances would you want to say b1 ||= b2; b1 &&= b2;

Should implicitly generated assignment operators be & ref-qualified?

北慕城南 提交于 2019-11-27 22:23:19
The following code compiles without problem on gcc 4.8.1: #include <utility> struct foo { }; int main() { foo bar; foo() = bar; foo() = std::move( bar ); } It seems the implicitly generated assignment operators for foo are not & ref-qualified and so can be invoked on rvalues. Is this correct according to the standard? If so, what reason is there for not requiring implicitly generated assignment operators to be & ref-qualified? Why doesn't the standard require the following to be generated? struct foo { foo & operator=( foo const & ) &; foo & operator=( foo && ) &; }; Well, there are certain

Has anyone found the need to declare the return parameter of a copy assignment operator const?

五迷三道 提交于 2019-11-27 20:59:54
问题 The copy assignment operator has the usual signature: my_class & operator = (my_class const & rhs); Does the following signature have any practical use? my_class const & operator = (my_class const & rhs); You can only define one or the other, but not both. 回答1: The principle reason to make the return type of copy-assignment a non-const reference is that it is a requirement for "Assignable" in the standard. If you make the return type a const reference then your class won't meet the

What does `let 5 = 10` do? Is it not an assignment operation?

走远了吗. 提交于 2019-11-27 19:59:57
If I say let 5 = 10 , why does 5 + 1 return 6 instead of 11 ? When you say let 5 = 10 it's not a redefinition of 5, it's a pattern matching, the same which occurs when you say foo 5 = undefined ... foo 10 ... The pattern simply fails if it's ever matched. In let-expressions the match is lazy. This means the match is only being done when a variable bound by it is evaluated. This allows us to write things like let foo = undefined in 10 In your expression, no variable is bound, so the pattern is never matched. Arguably such patterns with no variables make no sense in let-bindings and should be

assignment of class with const member

谁说胖子不能爱 提交于 2019-11-27 19:23:36
问题 Consider the following code: struct s { const int id; s(int _id): id(_id) {} }; // ... vector<s> v; v.push_back(s(1)); I get a compiler error that 'const int id' cannot use default assignment operator. Q1. Why does push_back() need an assignment operator? A1. Because the current c++ standard says so. Q2. What should I do? I don't want to give up the const specifier I want the data to be copied A2. I will use smart pointers. Q3. I came up with a "solution", which seems rather insane: s&

Low level details of C/C++ assignment operator implementation. What does it return?

若如初见. 提交于 2019-11-27 18:31:07
问题 I m a total newbie to a C++ world (and C too). And don't know all its details. But one thing really bothers me. It is constructions like : while (a=b) {...} .As I understand this magic works because assignment operator in C and C++ returns something. So the questions: what does it return? Is this a documented thing?Does it work the same in C and C++. Low level details about assignment operator and its implementation in both C and C++ (if there is a difference) will be very appreciated! I hope

What does an ampersand after this assignment operator mean?

左心房为你撑大大i 提交于 2019-11-27 17:35:47
I was reading through this nice answer regarding the "Rule-of-five" and I've noticed something that I don't recall seeing before: class C { ... C& operator=(const C&) & = default; C& operator=(C&&) & = default; ... }; What is the purpose of the & character placed in front of = default for the copy assignment operator and for the move assignment operator? Does anyone have a reference for this? It's part of a feature allowing C++11 non-static member functions to differentiate between whether they are being called on an lvalues or rvalues. In the above case, the copy assignment operator being

C++ copy-construct construct-and-assign question

扶醉桌前 提交于 2019-11-27 15:38:58
Here is an extract from item 56 of the book "C++ Gotchas": It's not uncommon to see a simple initialization of a Y object written any of three different ways, as if they were equivalent. Y a( 1066 ); Y b = Y(1066); Y c = 1066; In point of fact, all three of these initializations will probably result in the same object code being generated, but they're not equivalent. The initialization of a is known as a direct initialization, and it does precisely what one might expect. The initialization is accomplished through a direct invocation of Y::Y(int). The initializations of b and c are more complex

Should the Copy-and-Swap Idiom become the Copy-and-Move Idiom in C++11?

不打扰是莪最后的温柔 提交于 2019-11-27 13:10:30
As explained in this answer , the copy-and-swap idiom is implemented as follows: class MyClass { private: BigClass data; UnmovableClass *dataPtr; public: MyClass() : data(), dataPtr(new UnmovableClass) { } MyClass(const MyClass& other) : data(other.data), dataPtr(new UnmovableClass(*other.dataPtr)) { } MyClass(MyClass&& other) : data(std::move(other.data)), dataPtr(other.dataPtr) { other.dataPtr= nullptr; } ~MyClass() { delete dataPtr; } friend void swap(MyClass& first, MyClass& second) { using std::swap; swap(first.data, other.data); swap(first.dataPtr, other.dataPtr); } MyClass& operator=

C++ why the assignment operator should return a const ref in order to avoid (a=b)=c

孤街浪徒 提交于 2019-11-27 13:10:00
I am reading a book about C++ and more precisely about the operator overloading. The example is the following: const Array &Array::operator=(const Array &right) { // check self-assignment // if not self- assignment do the copying return *this; //enables x=y=z } The explanation provided by the book about returning const ref instead of ref is to avoid assignments such as (x=y)=z. I don't understand why we should avoid this. I understand that x=y is evaluated first in this example and since it returns a const reference the =z part cannot be executed. But why? (x=y) means x.operator=(y) , which