免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2194 | 回复: 8

[C++] lamba函数的bug? [复制链接]

论坛徽章:
0
发表于 2015-08-25 09:59 |显示全部楼层
如下代码,lambda函数的引用类型参数必须加const才能编译通过,这个是编译器bug吗?
  1. #include <thread>

  2. struct test {
  3.         test() {}
  4. };

  5. int main() {
  6.         // auto f = [](test& a) { };  // Error!
  7.         auto f = [](const test& a) { }; // OK
  8.         test a;
  9.         std::thread *t = new std::thread(f, a);
  10. }
复制代码

论坛徽章:
0
发表于 2015-08-25 10:21 |显示全部楼层
刚用vs2015 测试一下, 不写const 可以正常编译、运行。

论坛徽章:
0
发表于 2015-08-25 10:36 |显示全部楼层
mingyuejingque 发表于 2015-08-25 10:21
刚用vs2015 测试一下, 不写const 可以正常编译、运行。


我用最新版的g++和clang++都不行,难道这俩编译器都有这个bug?

论坛徽章:
0
发表于 2015-08-25 10:38 |显示全部楼层
~ $ g++ l.cc -std=c++17 -pthread
In file included from /usr/local/opt/gcc/include/c++/5.2.1/thread:39:0,
                 from l.cc:2:
/usr/local/opt/gcc/include/c++/5.2.1/functional: In instantiation of ‘struct std::_Bind_simple<main()::<lambda(test&)>(test)>’:
/usr/local/opt/gcc/include/c++/5.2.1/thread:137:59:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = main()::<lambda(test&)>&; _Args = {test&}]’
l.cc:12:39:   required from here
/usr/local/opt/gcc/include/c++/5.2.1/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<main()::<lambda(test&)>(test)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/local/opt/gcc/include/c++/5.2.1/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<main()::<lambda(test&)>(test)>’
         _M_invoke(_Index_tuple<_Indices...>)

论坛徽章:
0
发表于 2015-08-25 10:40 |显示全部楼层
~ $ clang++ l.cc -std=c++1z -pthread
In file included from l.cc:2:
In file included from /opt/clang/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.1/../../../../include/c++/5.2.1/thread:39:
/opt/clang/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.1/../../../../include/c++/5.2.1/functional:1505:56: error: no type named 'type' in 'std::result_of<(lambda at l.cc:9:11) (test)>'
      typedef typename result_of<_Callable(_Args...)>::type result_type;
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/opt/clang/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.1/../../../../include/c++/5.2.1/thread:137:41: note: in instantiation of template class 'std::_Bind_simple<(lambda at l.cc:9:11) (test)>'
      requested here
        _M_start_thread(_M_make_routine(std::__bind_simple(
                                        ^
l.cc:12:23: note: in instantiation of function template specialization 'std::thread::thread<(lambda at l.cc:9:11) &, test &>' requested here
        std::thread *t = new std::thread(f, a);
                             ^
In file included from l.cc:2:
In file included from /opt/clang/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.1/../../../../include/c++/5.2.1/thread:39:
/opt/clang/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.1/../../../../include/c++/5.2.1/functional:1525:50: error: no type named 'type' in 'std::result_of<(lambda at l.cc:9:11) (test)>'
        typename result_of<_Callable(_Args...)>::type
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
2 errors generated.

论坛徽章:
0
发表于 2015-08-25 10:40 |显示全部楼层
~ $ clang++ l.cc -std=c++1z -pthread -stdlib=libc++
In file included from l.cc:2:
/usr/local/opt/clang/bin/../include/c++/v1/thread:337:5: error: attempt to use a deleted function
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    ^
/usr/local/opt/clang/bin/../include/c++/v1/thread:347:5: note: in instantiation of function template specialization 'std::__1::__thread_execute<(lambda at l.cc:9:11), test, 1>' requested here
    __thread_execute(*__p, _Index());
    ^
/usr/local/opt/clang/bin/../include/c++/v1/thread:359:42: note: in instantiation of function template specialization 'std::__1::__thread_proxy<std::__1::tuple<(lambda at l.cc:9:11), test> >' requested
      here
    int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
                                         ^
l.cc:12:23: note: in instantiation of function template specialization 'std::__1::thread::thread<(lambda at l.cc:9:11) &, test &, void>' requested here
        std::thread *t = new std::thread(f, a);
                             ^
/usr/local/opt/clang/bin/../include/c++/v1/type_traits:1069:5: note: '~__nat' has been explicitly marked deleted here
    ~__nat() = delete;
    ^
1 error generated.

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
发表于 2015-08-25 10:52 |显示全部楼层
new std::thread(f, std::ref(a));

评分

参与人数 1可用积分 +5 收起 理由
windoze + 5 赞一个!

查看全部评分

论坛徽章:
0
发表于 2015-08-25 11:40 |显示全部楼层
bruceteen 发表于 2015-08-25 10:52
new std::thread(f, std::ref(a));


高!

论坛徽章:
2
2015亚冠之平阳省
日期:2015-07-08 10:15:132015亚冠之广州恒大
日期:2015-08-11 10:11:23
发表于 2015-08-25 12:36 |显示全部楼层
能解释解释是为什么吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP