ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 我的书对lambda返回类型的讨论是错误的吗?

2019-09-27 22:07:10  阅读:197  来源: 互联网

标签:c c11 lambda clang llvm


我的书说:

Lambdas with function bodies that contain anything other than a single return statement that do not specify a return type return void.

但是这个:

auto f = []{
  int i=0; i++;
  return std::string("foo");
};
std::cout << f() << std::endl;

实际上编译并打印出“foo”,但是lambda expr不仅仅包含一个return语句,因此它应该返回void,因为它不会手动指定“ – > std :: string”作为返回类型.

这里发生了什么?

我在基于Clang 3.2的最新Xcode 4.6中使用Apple的编译器似乎:

clang –version

Apple LLVM 4.2版(clang-425.0.24)(基于LLVM 3.2svn)
目标:x86_64-apple-darwin12.2.0
线程模型:posix

解决方法:

该书准确地反映了该标准的n3290草案中的规则.也许你的编译器实现了不同的草案.

在第5.1.2p4节中,草案如下

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, array-to-pointer conversion, and function-to-pointer conversion;
  • otherwise, void.

语法结构attribute-specifier-seq可以是alignas或double-bracketed属性.不是变量声明.

n3485草案,随后发布了C11(即它正在向C 1y进行的工作),包含相同的措辞.我不知道在某些草案中是否有一个不同于n3290的规则.

标签:c,c11,lambda,clang,llvm
来源: https://codeday.me/bug/20190927/1824805.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有