免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4862 | 回复: 2
打印 上一主题 下一主题

[C++] make_shared编译的一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-03-18 12:18 |只看该作者 |倒序浏览
我用C++11的语法,如果尝试下面的代码,编译没有问题:

  1. shared_ptr<FILE> fp(fopen("d:\\abc.txt", "w"),fclose);
复制代码
但是如果我改成了make_shared函数模板,就会有编译问题

  1. shared_ptr<FILE> fp = make_shared<FILE>(fopen("d:\\abc.txt", "w"),fclose);
复制代码
到底哪里出了问题,代码应该怎么改? 多谢。
-----------------------------------------
GCC4.8的编译错误是如下:
||=== Build: Debug in m1 (compiler: GNU GCC Compiler) ===|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\new_allocator.h||In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = _iobuf; _Args = {_iobuf*, int (__attribute__((__cdecl__)) &(_iobuf*)}; _Tp = _iobuf]'
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\alloc_traits.h|254|required from 'static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = _iobuf; _Args = {_iobuf*, int (__attribute__((__cdecl__)) &(_iobuf*)}; _Alloc = std::allocator<_iobuf>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]'|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\alloc_traits.h|393|required from 'static decltype (_S_construct(__a, __p, (forward<_Args>(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = _iobuf; _Args = {_iobuf*, int (__attribute__((__cdecl__)) &(_iobuf*)}; _Alloc = std::allocator<_iobuf>; decltype (_S_construct(__a, __p, (forward<_Args>(std::allocator_traits::construct::__args)...)) = <type error>]'|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr_base.h|400|required from 'std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {_iobuf*, int (__attribute__((__cdecl__)) &(_iobuf*)}; _Tp = _iobuf; _Alloc = std::allocator<_iobuf>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\new_allocator.h|120|required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::_Sp_counted_ptr_inplace<_iobuf, std::allocator<_iobuf>, (__gnu_cxx::_Lock_policy)2u>; _Args = {const std::allocator<_iobuf>, _iobuf*, int (__attribute__((__cdecl__)) &(_iobuf*)}; _Tp = std::_Sp_counted_ptr_inplace<_iobuf, std::allocator<_iobuf>, (__gnu_cxx::_Lock_policy)2u>]'|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\alloc_traits.h|254|  [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr_base.h|523|required from 'std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = _iobuf; _Alloc = std::allocator<_iobuf>; _Args = {_iobuf*, int (__attribute__((__cdecl__)) &(_iobuf*)}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr_base.h|986|required from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<_iobuf>; _Args = {_iobuf*, int (__attribute__((__cdecl__)) &(_iobuf*)}; _Tp = _iobuf; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr.h|316|required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<_iobuf>; _Args = {_iobuf*, int (__attribute__((__cdecl__)) &(_iobuf*)}; _Tp = _iobuf]'|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr.h|598|required from 'std::shared_ptr<_Tp1> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = _iobuf; _Alloc = std::allocator<_iobuf>; _Args = {_iobuf*, int (__attribute__((__cdecl__)) &)(_iobuf*)}]'|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr.h|614|required from 'std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = _iobuf; _Args = {_iobuf*, int (__attribute__((__cdecl__)) &)(_iobuf*)}]'|
D:\Documents\gccprojects\m1\main.cpp|6|required from here|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\new_allocator.h|120|error: new initializer expression list treated as compound expression [-fpermissive]|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\new_allocator.h|120|error: no matching function for call to '_iobuf::_iobuf(int (__attribute__((__cdecl__)) &)(_iobuf*))'|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\new_allocator.h|120|note: candidates are
c:\program files (x86)\codeblocks\mingw\include\stdio.h|129|note: _iobuf::_iobuf()|
c:\program files (x86)\codeblocks\mingw\include\stdio.h|129|note:   candidate expects 0 arguments, 1 provided|
c:\program files (x86)\codeblocks\mingw\include\stdio.h|129|note: constexpr _iobuf::_iobuf(const _iobuf&)|
c:\program files (x86)\codeblocks\mingw\include\stdio.h|129|note:   no known conversion for argument 1 from 'int(_iobuf*)' to 'const _iobuf&'|
c:\program files (x86)\codeblocks\mingw\include\stdio.h|129|note: constexpr _iobuf::_iobuf(_iobuf&&)|
c:\program files (x86)\codeblocks\mingw\include\stdio.h|129|note:   no known conversion for argument 1 from 'int(_iobuf*)' to '_iobuf&&'|
||=== Build failed: 3 error(s), 11 warning(s) (0 minute(s), 0 second(s)) ===|

论坛徽章:
44
15-16赛季CBA联赛之浙江
日期:2021-10-11 02:03:59程序设计版块每日发帖之星
日期:2016-07-02 06:20:0015-16赛季CBA联赛之新疆
日期:2016-04-25 10:55:452016科比退役纪念章
日期:2016-04-23 00:51:2315-16赛季CBA联赛之山东
日期:2016-04-17 12:00:2815-16赛季CBA联赛之福建
日期:2016-04-12 15:21:2915-16赛季CBA联赛之辽宁
日期:2016-03-24 21:38:2715-16赛季CBA联赛之福建
日期:2016-03-18 12:13:4015-16赛季CBA联赛之佛山
日期:2016-02-05 00:55:2015-16赛季CBA联赛之佛山
日期:2016-02-04 21:11:3615-16赛季CBA联赛之天津
日期:2016-11-02 00:33:1215-16赛季CBA联赛之浙江
日期:2017-01-13 01:31:49
2 [报告]
发表于 2014-03-21 16:28 |只看该作者
这位同学,你看看文档里说make_shared的参数到底该是神马好不?

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
3 [报告]
发表于 2014-03-21 18:30 |只看该作者
回复 1# iwqueri


   make_shared的参数是针对类型T的(new 一个T类的对象并传递参数给其构造函数),就你的列子是FILE,而第一种方式,其参数是针对shared_ptr的构造函数参数的,两种用法的含义不一样。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP