Conversation
* wiki/wiki/cpp-tutorial/operators.md: Added a doc for operators in C++ Sign off: Magnus-Mage <>
|
Should be a good addition for operators introduction upto some advance stuff, but if its an overkill around the end, i can shorten it for better understanding |
jeremy-rifkin
left a comment
There was a problem hiding this comment.
Thanks for the work on this! Some comments below
| |Operator|Description|Example|Result| | ||
| |---|---|---|---| | ||
| |`&&`|Logical AND|`true && false`|`false`| | ||
| |`\|`|Logical OR|`true \| false`|`true`| |
There was a problem hiding this comment.
| |`\|`|Logical OR|`true \| false`|`true`| | |
| |`\|\|`|Logical OR|`true \| false`|`true`| |
|
|
||
| ## Assignment Operators | ||
|
|
||
| Assignment operators are used to assign values to variables. The basic assignment operator is `=`, but C++ provides compound assignment operators that combine arithmetic operations with assignment. The assignment is done from left to right, the value of left is assigned the value on right, in some cases after an operation. |
There was a problem hiding this comment.
| Assignment operators are used to assign values to variables. The basic assignment operator is `=`, but C++ provides compound assignment operators that combine arithmetic operations with assignment. The assignment is done from left to right, the value of left is assigned the value on right, in some cases after an operation. | |
| Assignment operators are used to assign values to variables. The basic assignment operator is `=`, but C++ provides compound assignment operators that combine arithmetic operations with assignment. The assignment is done from right to left, the value of the right hand side is assigned the left hand side, in some cases after an operation. |
|
|
||
| int array[10]; | ||
| int array_size = sizeof(array); // 40 (assuming int is 4 bytes) | ||
| int array_length = sizeof(array) / sizeof(array[0]); // 10 |
There was a problem hiding this comment.
Let's maybe omit the array example. It's a dated and error-prone example, not something we want to showcase to people who are learning.
| ```c++ | ||
| int a = (5, 10, 15); // a = 15 | ||
|
|
||
| int x = 1, y = 2; |
There was a problem hiding this comment.
In the context of teaching the comma operator, using a comma in declarations might be confusing. We also in general recommend against using commas in declarations like this.
| It's most commonly seen in `for` loops: | ||
|
|
||
| ```c++ | ||
| for (int i = 0, j = 10; i < j; ++i, --j) { |
|
|
||
| ## Experimenting with Operators | ||
|
|
||
| As you continue learning C++, you'll discover that operators can be used in creative and powerful ways. Some of this areas are operator overloading, combining operators, bitwise tricks, etc. |
There was a problem hiding this comment.
| As you continue learning C++, you'll discover that operators can be used in creative and powerful ways. Some of this areas are operator overloading, combining operators, bitwise tricks, etc. | |
| As you continue learning C++, you'll discover that operators can be used in creative and powerful ways. Some of these areas are operator overloading, combining operators, bitwise tricks, etc. |
| int b = 20; | ||
| int max = (a > b) ? a : b; // max = 20 | ||
|
|
||
| // This is Equivalent to: |
There was a problem hiding this comment.
| // This is Equivalent to: | |
| // This is equivalent to: |
| # Operators | ||
|
|
||
| ## What is an Operator? | ||
| In any programming language, the core standard provides simple ways to do various operations on numbers, variables, pointers, even functions with operator overloading. In C++, we are also provided with various operators that we can use to do different tasks. Usually they have the same symbols and methodology as elementary mathematics. We will discuss what are operators and how are they separated into groups by the C++ standard below. |
There was a problem hiding this comment.
Tone-wise, this is a bit heavy / textbook-like. We're trying to keep these relatively to the point (as done on other pages).
| - Implementing algorithms that leverage bitwise operations | ||
| - Creating your own classes with overloaded operators | ||
|
|
||
| Remember that readable code is often more important than clever tricks. Use operators in ways that make your intent clear to other programmers (or yourself in the future). |
There was a problem hiding this comment.
Per the writing guide "don't be preachy or lecture-like" / "don't be dismissive of common mistakes":
| Remember that readable code is often more important than clever tricks. Use operators in ways that make your intent clear to other programmers (or yourself in the future). | |
| We highly recommend writing code that is clear and readable to others (and your future self) and avoid clever tricks that can make code harder to read. |
| > [!NOTE] For Advanced Programmers | ||
| > | ||
| > Consider exploring topics like expression templates, SFINAE with operators, the spaceship operator (`<=>`) introduced in C++20, and how compilers optimize operator usage. The `constexpr` keyword can also be applied to operator overloads for compile-time evaluation. Understanding move semantics and perfect forwarding becomes crucial when overloading assignment operators and implementing efficient operator overloads. |
There was a problem hiding this comment.
I think this is too broad and generic, maybe just omit it
|
|
||
| > [!NOTE] | ||
| > | ||
| > For signed integers, right shift behavior is implementation-defined. Some systems perform arithmetic shift (sign extension), while others perform logical shift (zero fill). |
There was a problem hiding this comment.
There is nothing implementation-defined here. Right-shift for signed integers is an arithmetic right shift. https://eel.is/c++draft/expr.shift#3
| ### Ternary Operator (`? :`) | ||
|
|
||
| The ternary operator is a shorthand for `if-else` statements. It takes three operands. |
There was a problem hiding this comment.
It may be better to use the formal name "conditional operator". https://eel.is/c++draft/expr.cond
"Ternary operator" is colloquial.
No description provided.