Vlad from Moscow
Vlad from Moscow

Reputation: 311126

What is the reason for the existent difference between C and C++ relative to the unary arithmetic operator +

In C the unary plus operator is called unary arithmetic operator and may not be applied to pointers (the C Standard, 6.5.3.3 Unary arithmetic operators).

1 The operand of the unary + or - operator shall have arithmetic type; of the ~ operator, integer type; of the ! operator, scalar type.

Thus this program will not compile

#include <stdio.h>

int main(void) 
{
    int a = 10;
    int *pa = &a;

    printf( "%d\n", *+pa );

    return 0;
}

However in C++ the unary plus operator may be applied to pointers (the C++ Standard, 5.3.1 Unary operators)

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

And this program compiles successfully.

#include <iostream>

int main() 
{
    int a = 10;
    int *pa = &a;

    std::cout << *+pa << std::endl;

    return 0;
}

What is the reason for maintaining this difference between C and C++?


The question arose when I was answering the question Why size of int pointer is different of size of int array?. I was going to show how to convert an array to a pointer in the sizeof operator.

At first I wanted to write

sizeof( +array )

However this expression is invalid in C. So I had to write

sizeof( array + 0 )

and I found that there is such a difference between C and C++.:)

Upvotes: 8

Views: 483

Answers (2)

0x2333
0x2333

Reputation: 42

In my considerations:

C++ is a type of Object-Oriented Language. So every data type can be treated as a "Class".

In C int is one of "the basic data type of C". But in C++ we can consider int as a Class. Thus, In C++ int pointer and int array belong to the different classes. In C a int pointer variable stored another int variable's address. int array's name instead of the first element's address of that int array. So in C they have kind of the same meaning.

As for the unary opreator "+", I understand the C++ language as: Every class In C++ represents a set of stuff. Every stuff in the set has the same properties. And there's some operations can be done onto each stuff. Of course these operations are member functions of a class. Another character In C++ is that users can overload an operator. Overload means we can do the same operation on the different Classes. For example: A man is eating a burger. we can overload action "Eat" between cats and rat: A cat is Eating a rat.

So as the C++ standard say:"The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument." That's just a overload for unary operator + in Class unscoped enumeration and pointer type. "And The Result Is The Value Of The Argument"-> I guess that's the point.

Upvotes: -2

chqrlie
chqrlie

Reputation: 145307

Different languages may attach different semantics to the same syntax.

C and C++ are different languages with a common ancestor. C++ semantics look deceptively similar but are subtly different for some parts of the common syntax. Another curious case is this:

if (sizeof(char) == sizeof(int)) {
    printf("Hello embedded world\n");
} else {
    if (sizeof('a') == sizeof(char))
        printf("This is C++ code\n");
    if (sizeof('a') == sizeof(int))
        printf("This is C code\n");
}

The reason for C++ to have extended the C syntax in the case of unary + might be to allow for some extended numeric types to be implemented as pointers, or simply for reasons of symmetry.

As Jaa-c mentions in a comment, +p is a computed expression whereas p is a reference to p. You provided another example where + can be used to force expression context. The question is why did the original authors of the C language disallow unary + on non numeric types? Maybe a side effect of the original implementation of pcc.

Note that in Javascript, the unary + operator can be applied to non number types and operates as a conversion to number.

Upvotes: 1

Related Questions