What is the use of unary plus operator in pointer initialization?
The section 6.5.3.3 of C99 states:
1) The operand of the unary + or - operator shall have arithmetic type;
......2) The result of the unary + operator is the value of its (promoted)
operand. The integer promotions are performed on the operand, and the
result has the promoted type.
Pointers or arrays are not an arithmetic type.
What does a plus sign mean in front of a char array?
Built-in unary operator+
could take pointer type (but not array type) as its operand, so using it on an array causes array-to-pointer decay, then +"\n"
and +s
would return const char *
.
On the other hand, if you remove the usage of operator+
, you'll try to pass the array with type const char[2]
to os
, which leads to recursive invocation, as the error message tried to tell you.
For the built-in operator, expression must have arithmetic, unscoped enumeration, or pointer type. Integral promotion is performed on the operand if it has integral or unscoped enumeration type and determines the type of the result.
The built-in unary plus operator returns the value of its operand. The only situation where it is not a no-op is when the operand has integral type or unscoped enumeration type, which is changed by integral promotion, e.g, it converts
char
toint
or if the operand is subject to lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion.
What does int* p=+s; do?
Built-in operator+
could take pointer type as its operand, so passing the array s
to it causes array-to-pointer conversion and then the pointer int*
is returned. That means you might use +s
individually to get the pointer. (For this case it's superfluous; without operator+
it'll also decay to pointer and then assigned to p
.)
(emphasis mine)
The built-in unary plus operator returns the value of its operand. The only situation where it is not a no-op is when the operand has integral type or unscoped enumeration type, which is changed by integral promotion, e.g, it converts char to int or if the operand is subject to lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion.
Does the unary + operator have any practical use?
char ch = 'a';
std::cout << ch << '\n';
std::cout << +ch << '\n';
The first insertion writes the character a
to cout
. The second insertion writes the numeric value of ch
to cout
. But that's a bit obscure; it relies on the compiler applying integral promotions for the +
operator.
Unary plus (+) against literal string
Unary +
can be applied to arithmetic type values, unscoped enumeration values and pointer values because ...
the C++ standard defines it that way, in C++11 §5.3.1/7.
In this case the string literal, which is of type array of char const
, decays to pointer to char const
.
It's always a good idea to look at the documentation when one wonders about the functionality of something.
“The operand of the unary +
operator shall have arithmetic, unscoped enumeration, or pointer type and the
result is the value of the argument. Integral promotion is performed on integral or enumeration operands.
The type of the result is the type of the promoted operand.”
+ unary operator in javascript
but i still can't figure why console.log(+a) return 3 the first time.
The value of a
is 3
at that point.
The previous line, -a
, takes the value of a
, negates it and passes it to console.log
. It doesn't assign the changed value back to a
.
Plus char ('+') before constant
It's the unary plus (+) operator.
It precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.
const x = 1;
const y = -1;
console.log(+x);
// expected output: 1
console.log(+y);
// expected output: -1
console.log(+'');
// expected output: 0
console.log(+true);
// expected output: 1
console.log(+false);
// expected output: 0
console.log(+'hello');
// expected output: NaN
Related Topics
Get Key Press in Windows Console
How to Benchmark Boost Spirit Parser
Std::Optional - Construct Empty with {} or Std::Nullopt
Video Processing with Opencv in iOS Swift Project
Why Is It a Compile Error to Assign the Address of an Array to a Pointer "My_Pointer = &My_Array"
Explicit Call to Destructor Is Not Destroying My Object Why
What Do the C and C++ Standards Say About Bit-Level Integer Representation and Manipulation
How to Apply Content Not Explicitly Cited from the Normative References to the C++ Standard
Intersection of Two 'Std::Map'S
How to Generate Type Library from Unmanaged Com Dll
Redirect Both Cout and Stdout to a String in C++ for Unit Testing
What Is the Array Form of 'Delete'
Can Cython Code Be Compiled to a Dll So C++ Application Can Call It
Factory Method Implementation - C++
Mingw Error: 'Thread' Is Not a Member of 'Std'
Boost::Spirit How to Parse and Call C++ Function-Like Expressions