1

As the title stated. The code is compiled using GNU c++2a

int main(){
  (+[](){});
  return 0;
}

Compiles fine. However, the following code generates warning: value computed is not used [-Wunused-value]

int main(){
  +[](){};
  return 0;
}

Further question is: my understanding about the expression [](){} is, it returns an r-value object std::function<void()>. While, I don't know there is a unary operator +, when the + applies on any r-value, should it be a compile error generated? Or maybe because of the operator precedence, the expression is interpreted in another way?

cigien
  • 57,834
  • 11
  • 73
  • 112
r0n9
  • 2,505
  • 1
  • 29
  • 43
  • For the "+" see https://stackoverflow.com/questions/17822131/resolving-ambiguous-overload-on-function-pointer-and-stdfunction-for-a-lambda – Mike Vine Nov 26 '20 at 23:27
  • "it returns an r-value object `std::function`" - no, it evaluates to an implementation-defined type (*"unnamed non-union class type — called the closure type"*), which can be used to construct a `std::function` (or assigned to one). – Tony Delroy Nov 26 '20 at 23:27

1 Answers1

3

{} is, it returns an r-value object std::function<void()>

No, it creates a lambda/closure which is its own kind of thing. There are cases when that is turned into a std::function, but what you're actually getting is much more similar to a functor (a class that implements operator()) than a std::function - which is a type-erased holder for things which can be called.

The + sign forces the closure to be turned into a function pointer (because that's the only thing thats "easy" to convert to which can have a unary + applied to it), which when wrapped in () "uses" the pointer value in a list context. Without that, you compute a function pointer but then discard it immediately. It's telling you that your + sign is silly.

xaxxon
  • 19,189
  • 5
  • 50
  • 80