Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 唯一\u ptr和指定解构器_C++_C++11_Smart Pointers_Unique Ptr - Fatal编程技术网

C++ 唯一\u ptr和指定解构器

C++ 唯一\u ptr和指定解构器,c++,c++11,smart-pointers,unique-ptr,C++,C++11,Smart Pointers,Unique Ptr,用clang++--std=c++11 file.cpp编译我的程序时,行std::unique_ptr管道(popen(cmd.c_str(),“r”),pclose)抛出错误 memdiff.cpp:11:27: error: no matching constructor for initialization of 'std::unique_ptr<FILE>' std::unique_ptr<FILE> pipe(popen(cmd.c_str

clang++--std=c++11 file.cpp
编译我的程序时,行
std::unique_ptr管道(popen(cmd.c_str(),“r”),pclose)抛出错误

memdiff.cpp:11:27: error: no matching constructor for initialization of
      'std::unique_ptr<FILE>'
    std::unique_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
                          ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2530:31: note:
      candidate constructor not viable: no known conversion from 'int (FILE *)'
      to 'const std::__1::default_delete<__sFILE>' for 2nd argument
    _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
                              ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2537:31: note:
      candidate constructor not viable: no known conversion from 'int (FILE *)'
      to 'typename remove_reference<deleter_type>::type' (aka
      'std::__1::default_delete<__sFILE>') for 2nd argument
    _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_ref...
                              ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2547:9: note:
      candidate template ignored: could not match 'unique_ptr<type-parameter-0-0,
      type-parameter-0-1>' against '__sFILE *'
        unique_ptr(unique_ptr<_Up, _Ep>&& __u,
        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2562:35: note:
      candidate template ignored: could not match 'auto_ptr<type-parameter-0-0>'
      against '__sFILE *'
        _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
                                  ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2516:49: note:
      candidate constructor not viable: requires 1 argument, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
                                                ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2522:40: note:
      candidate constructor not viable: requires single argument '__p', but 2
      arguments were provided
    _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
                                       ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2543:31: note:
      candidate constructor not viable: requires single argument '__u', but 2
      arguments were provided
    _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
                              ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2487:29: note:
      candidate constructor (the implicit copy constructor) not viable: requires
      1 argument, but 2 were provided
class _LIBCPP_TYPE_VIS_ONLY unique_ptr
                            ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2510:49: note:
      candidate constructor not viable: requires 0 arguments, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
                                                ^
1 error generated.
memdiff.cpp:11:27:错误:没有用于初始化的匹配构造函数
'std::unique_ptr'
标准:独特的管道(popen(cmd.c_str(),“r”),pclose);
^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/memory:2530:31:注意:
候选构造函数不可行:没有从“int(FILE*)”进行的已知转换
到第二个参数的“const std::_1::default_delete”
_LIBCPP_INLINE_VISIBILITY unique_ptr(指针uu p,类型名条件)<
^
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/memory:2537:31:注意:
候选构造函数不可行:没有从“int(FILE*)”进行的已知转换
到“typename remove_reference::type”(又名
第二个参数的“std::_1::default_delete”)
_LIBCPP_INLINE_VISIBILITY unique_ptr(指针uu p,类型名remove_ref。。。
^
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/memory:2547:9:注意:
已忽略候选模板:无法将“unique\u ptr”与“unique\u sFILE*”匹配
独一无二的,
^
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/内存:2562:35:注意:
已忽略候选模板:无法匹配“自动检查”
反对“\uu sFILE*”
_LIBCPP_INLINE_VISIBILITY unique_ptr(自动_ptr&&&p,
^
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/memory:2516:49:注意:
候选构造函数不可行:需要1个参数,但提供了2个
_LIBCPP_INLINE_VISIBILITY_LIBCPP_CONSTEXPR unique_ptr(nullptr_t)_无例外
^
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/memory:2522:40:注意:
候选构造函数不可行:需要单个参数“\up”,但需要2个
提供了论据
_LIBCPP_INLINE_VISIBILITY显式唯一_ptr(指针_p)_NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/memory:2543:31:注意:
候选构造函数不可行:需要单个参数“\u”,但需要2个
提供了论据
_LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&&&uu)_无例外
^
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/memory:2487:29:注意:
候选构造函数(隐式副本构造函数)不可行:需要
1个参数,但提供了2个
类_LIBCPP_TYPE_VIS_ONLY unique_ptr
^
/Library/Developer/CommandLineTools/usr/bin/./include/c++/v1/memory:2510:49:注意:
候选构造函数不可行:需要0个参数,但提供了2个
_LIBCPP_INLINE_VISIBILITY_LIBCPP_CONSTEXPR unique_ptr()_NOEXCEPT
^
生成1个错误。

如果我从一个唯一的\u ptr切换到一个共享的\u ptr,我的程序将编译。为什么一个构造函数工作而一个不工作,以及如何修复它?

正如第一条注释中正确指出的那样:使用唯一的\u ptr,必须将删除器的类型指定为第二个模板参数

但是,它应该是一个函数指针:

std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
std::unique_ptr管道(popen(cmd.c_str(),“r”),pclose);

使用
unique\u ptr
,必须将删除器的类型指定为第二个模板参数。将其传递给构造函数是不够的。这应该可以工作:
std::unique\u ptr pipe(…,pclose);
工作正常。谢谢!解构器虽然我真的很喜欢这个声音,但正确的术语仍然是析构函数