Skip to content

Operators article#76

Open
Magnus-Mage wants to merge 3 commits intoTCCPP:mainfrom
Magnus-Mage:operators-article
Open

Operators article#76
Magnus-Mage wants to merge 3 commits intoTCCPP:mainfrom
Magnus-Mage:operators-article

Conversation

@Magnus-Mage
Copy link

No description provided.

Magnus-Mage and others added 3 commits January 15, 2026 07:56
	* wiki/wiki/cpp-tutorial/operators.md: Added a doc for operators in C++

Sign off: Magnus-Mage <>
@Magnus-Mage
Copy link
Author

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

Copy link
Member

@jeremy-rifkin jeremy-rifkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work on this! Some comments below

|Operator|Description|Example|Result|
|---|---|---|---|
|`&&`|Logical AND|`true && false`|`false`|
|`\|`|Logical OR|`true \| false`|`true`|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
|`\|`|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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here


## 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the writing guide "don't be preachy or lecture-like" / "don't be dismissive of common mistakes":

Suggested change
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.

Comment on lines +477 to +479
> [!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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is nothing implementation-defined here. Right-shift for signed integers is an arithmetic right shift. https://eel.is/c++draft/expr.shift#3

Comment on lines +339 to +341
### Ternary Operator (`? :`)

The ternary operator is a shorthand for `if-else` statements. It takes three operands.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be better to use the formal name "conditional operator". https://eel.is/c++draft/expr.cond

"Ternary operator" is colloquial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants