39

What does the following do? I thought + was for integer promotion only.

char c[20] = "hello";
foo(+c);
foo(+"hello");
user4014723
  • 391
  • 3
  • 3
  • 10
    The freakest case of this are *positive lambdas*, which do exactly the same to decay the lambda into a function pointer: `+[](){}` – Manu343726 Sep 07 '14 at 07:00
  • 1
    @Manu343726 In your expression, is the resulting function pointer valid? Or has the anonymous lambda function already gone out of scope? Phrased in code, is `(* +[](){} )()` valid? – Ponkadoodle Dec 21 '14 at 07:58

2 Answers2

40

It forces the array to decay to a pointer, as indirectly stated in §5.3.1 [expr.unary.op]/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.

You might not see it at first, but since an array is not one of the types listed, it must be converted to a pointer in order to fit. From there, the value of the pointer is returned.

In both cases, a foo(const char *) would be chosen over a foo(const char(&)[N]). For some examples of useful things you can use unary plus for, see this answer. Included are converting an enum type to an integer and getting around a linking issue. As you say, it can also be used for integral promotion. For example, unsigned char byte = getByte(); std::cout << +byte; will print the numerical value and never the character.


A straightforward example is:

char a[42];
cout << sizeof(a) << endl;  // prints 42
cout << sizeof(+a) << endl; // prints 4
Community
  • 1
  • 1
chris
  • 60,560
  • 13
  • 143
  • 205
  • `will print the numerical value and never the character`... unless `sizeof(char) == sizeof(int)`, right? – fredoverflow Sep 06 '14 at 15:18
  • 9
    @FredOverflow `char` and `unsigned char` always get promoted to `int` or `unsigned int`, even if they already have the same size. –  Sep 06 '14 at 15:21
  • Is this a common idiom in C++, or is there a better way to write it (e.g. a cast) that makes it more obvious what is going on? – Nate Eldredge Sep 07 '14 at 04:24
  • 1
    @NateEldredge, I've never seen it used for arrays, but for places in code where there are quite a few cases of wanting an integer from an enum or smaller integer, the casts really add up in the space they take, so it does do a nice job there. I would say it's really most debatable when there's the odd instance or two where it could be used. – chris Sep 07 '14 at 04:33
  • VS2017 prints "42" for both cases – Igor Oct 04 '18 at 07:49
  • it's just a short way to do `cout << sizeof(a+1-1)` by implying the same calculative intent which results in decay, and obviously it can't use a unary minus, but unary plus yields the same value as the value without the unary plus. It's the shortest way to yield the address while performing pointer arithmetic, which is what causes the decay – Lewis Kelsey Feb 28 '21 at 05:38
23

Unary + is not only defined for numeric types, but also for pointer types. It is not defined, however, for arrays.

So, foo(+c) forces c to be converted to a pointer to its first element. If foo is a function defined as taking a char *, that wouldn't be necessary: it would already happen implicitly. It can matter when you have overloads (possibly via a template function), though.