Undefined Behavior with the C++0x Closure: I

后端 未结 3 1199
野趣味
野趣味 2021-01-19 19:17

Consider the example:

#include 
#include      // std::function
#include         // std::vector
#include 

        
3条回答
  •  臣服心动
    2021-01-19 19:44

    It seems, that in your example trailing-return-type cannot be omitted. Here is excerpt from standard (5.1.2 Lambda expressions):

    If a lambda-expression does not include a trailing-return-type, it is as if the trailing-return-type denotes the following type: — if the compound-statement is of the form { attribute-specifier-seq return expression ; } the type of the returned expression after lvalue-to-rvalue conversion (4.1), array-to-pointer conversion (4.2), and function-to-pointer conversion (4.3); — otherwise, void.

    Returned value in your example cannot be used for conversions mentioned above. Following code with explicitely added return type compiles in VS 2010:

    auto adder = [] (int x) -> std::function {
      return [=]( int y ) {
        return x + y;
      };
    };
    

提交回复
热议问题