带有lambda自定义删除器的std :: unique_ptr不会编译

时间:2016-10-03 00:51:46

标签: c++ c++11 lambda smart-pointers core-foundation

无法在以下代码中找到问题

std::unique_ptr<CFStringRef, std::function<void(CFStringRef)>>
cfstr(CFStringCreateWithCString(NULL, str, kCFStringEncodingUTF8),
                                 [](CFStringRef obj){
                                     CFRelease(obj);
                                 });

CFStringCreateWithCString应根据CFStringCreateWithCString documentation

返回正确的类型

错误:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=c++11 -stdlib=libc++ -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wno-unused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.7 -g -fvisibility-inlines-hidden -Wno-sign-conversion -D_DEBUG -DDEBUG -MMD -MT dependencies -MF -c DynamicStore.cpp -o DynamicStore.o

DynamicStore.cpp:29:5: error: no matching constructor for initialization of 'std::unique_ptr<CFStringRef, std::function<void (CFStringRef)> >'
    cfstr((CFStringRef)CFStringCreateWithCString(NULL, str, kCFStringEncodingUTF8),

main.cpp:73:14: note: in instantiation of function template specialization 'std::__1::for_each<std::__1::__wrap_iter<const std::__1::sub_match<const char *> *>, (lambda at main.cpp:73:45)>' requested here
        std::for_each(cm.begin(), cm.end(), [](const std::cmatch &s){
             ^
main.cpp:73:45: note: candidate function not viable: no known conversion from 'const std::__1::sub_match<const char *>' to 'const std::cmatch' (aka 'const match_results<const char *>') for 1st argument
        std::for_each(cm.begin(), cm.end(), [](const std::cmatch &s){
                                            ^
main.cpp:73:45: note: conversion candidate of type 'void (*)(const std::cmatch &)'

3 个答案:

答案 0 :(得分:3)

建立@NulledPointer引用:

struct CFStringRefDeleter {
    using pointer = CFStringRef;
    void operator()(CFStringRef ref) {
         CFRelease(ref);
    }
};
using CFStr_t = std::unique_ptr<CFStringRef, CFStringRefDeleter>;
CFStr_t cfstr(CFStringCreateWithCString(NULL, str, kCFStringEncodingUTF8));
CFStringRef obj = cfstr.get();

答案 1 :(得分:2)

不确定是否足够但是...如果我没错,析构函数使用指向该类型的指针,而不是类型的对象

我的意思是......你应该将类型定义为

std::unique_ptr<CFStringRef, std::function<void(CFStringRef * )>>
                                                            ^    
                            note the pointer ---------------|

并且lambda应该接收(并使用)指针

[](CFStringRef * pObj){ CFRelease(*pObj) /* ? */ };

答案 2 :(得分:0)

将“why”部分添加到max的答案:

根据unique_ptr doc

  

-Deleter必须是FunctionObject或lvalue引用的FunctionObject或lvalue函数引用,可以用一个函数调用   unique_ptr<T, Deleter>::pointer

类型的参数

解释unique_ptr<T, Deleter>::pointer的地方member types

  

unique_ptr<T, Deleter>::pointer = std::remove_reference<Deleter>::type::pointer如果是那种类型   存在,否则T*。必须满足NullablePointer