What Is the "" Operator For

What is the === operator for?

In PHP, JavaScript, ECMAScript, ActionScript 3.0, and a number of other similar, dynamic languages, there are two types of equality checks: == (non-strict equality) and === (strict equality). To show an example:

5 == "5"   // yep, these are equal, because "5" becomes 5 when converted to int
5 === "5" // nope, these have a different type

Basically, whenever you use ==, you risk automatic type conversions. Using === ensures that the values are logically equal AND the types of the objects are also equal.

What is the ?? operator for?

It's called the "null coalescing operator" and works something like this:

Instead of doing:

int? number = null;
int result = number == null ? 0 : number;

You can now just do:

int result = number ?? 0;

What is the -- operator in C++?

--> is not an operator. It is in fact two separate operators, -- and >.

The conditional's code decrements x, while returning x's original (not decremented) value, and then compares the original value with 0 using the > operator.

To better understand, the statement could be written as follows:

while( (x--) > 0 )

What is the operator for in C++?

You didn't spell it out exactly, but I believe that your confusion is that you think that std::cout is a function, and you're wondering why you don't just call it like this:

std::cout("Hello World");

Well, std::cout is not a function. The function in this statement is operator<<.

std::cout << "Hello World";

Or, more specifically, the function is std::ostream::operator<<(const char*).

The thing you need to understand is that operators are just functions with an alternative calling syntax. operator<< is overloaded as a member function of std::ostream, and std::cout is an object of std::ostream. So this:

std::cout << "Hello World";

Is an alternative way to call this:

std::cout.operator<<("Hello World");

Note that operator<< is a binary operator, which means it takes two arguments, if it's declared as a free function, and one argument if it is declared as a member function. When you use the alternative calling syntax, the object on the left is the first argument, and the object on the right is the second argument. In the case where it is declared as a member function, as it is in this case, the object on the left is the calling object, and the object on the right is the argument.

Here's what it would look like if it were declared as a free function:

operator<<(std::cout, "Hello World");

But, whether it is declared as a member or a free function, you can still use the same alternative calling syntax.

What is the operator in C++?

Those are user-defined literals. They allow you to create stuff like std::string, std::chrono::durations or any user defined type (you can make your own literals) in place:

auto str = "Hello"s; // str is std::string("Hello")
auto sec = 5s; // sec is 5 std::chrono::seconds

A list of the literal-operators provided by the standard library and their documentation can be found at the bottom of the documentation page I linked.

What does the operator do in C++?

The primary usage of this operator"" is the creation of user-defined-literals. From the reference:

Allows integer, floating-point, character, and string literals to produce objects of user-defined type by defining a user-defined suffix.


You can call this operator as you would any other overloaded operator:

std::cout << 42.5_deg;                // with convenient operator syntax
std::cout << operator"" _deg(42.5); // with an explicit call

Not entirely unrelated: as pointed out in the comments to your question, this example is badly named. It takes in degrees and returns radians, so it should probably be named operator"" _rads. The purpose of UDLs is to have convenient, easy to read syntax, and a function that lies about what it does actively undermines that.


You can use this operator to do pretty much any computation you want (with constraints on the type, and number of the arguments passed in, similar to other operators), for example:

constexpr long double operator"" _plus_one ( long double n )
{
return n + 1;
}

Though the usage of this operator would still be the same as above.

Here's a demo.

What is the := operator?

In all languages that support an operator := it means assignment.

  • In languages that support an operator :=, the = operator usually means an equality comparison.
  • In languages where = means assignment, == is typically used for equality comparison.

does := mean =?

I can't recall any languages where := means the same as =.


In MySQL := and = are both used for assignment, however they are not interchangeable and selecting the correct one depends on the context. To make matters more confusing the = operator is also used for comparison. The interpretation of = as either assignment or comparison also depends on context.

What is the ?? operator?

Nil-Coalescing Operator (??)

The nil-coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.

?? - indicates default value assignment, when your variable has a nil value.

Consider following example:

var int a = nil    // var a with nil value assignment

var int b = a ?? 1 // first trying to get value from a. If var a has nil then it will try assign value 1.

print("b = \(b)")

result: b = 1



Related Topics



Leave a reply



Submit