Compiler warning: lambda return type cannot be deduced

后端 未结 2 1983
小鲜肉
小鲜肉 2020-12-19 16:36

Consider this example:

#include 
#include 

int main()
{
    std::string str = \"abcde4fghijk4l5mnopqrs6t8uvwxyz\";
    std:         


        
2条回答
  •  伪装坚强ぢ
    2020-12-19 16:49

    As @ildjarn says in his comment, your code is simply ill-formed according to the standard.

    §5.1.2 [expr.prim.lambda] p4

    [...] 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-seqopt 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.

    [...]

    That's it, basically if the code inside the curly brackets (called compund-statement in the standard) is anything but return some_expr;, the standard says the return type is undeducible and you get a void return type.

提交回复
热议问题