2013-02-16 1 views
1

다음 코드를 컴파일하는 데 실패했습니다. 꼭 그래야하지만 지금 당장은 저를 피합니다. 대단히 도움이나 제안을 부탁드립니다! std :: unique_ptr을 사용하는 정적 함수 deleter를 사용하는 중 오류

#include <atomic> 
#include <memory> 

struct MyClass { 

static void free_lock(std::atomic<int>** lck) { (*lck)->store(0); } 

typedef std::unique_ptr<std::atomic<int>*, decltype(&MyClass::free_lock)> lock_scope; 

static lock_scope get_lock() { 
    static std::atomic<int> lck(0); 
    int ref = 0; 
    return lock_scope(&lck, &MyClass::free_lock); 
} 

}; 

다음과 같은 오류 메시지

 
Compilation finished with errors: 
source.cpp:13:23: error: no matching constructor for initialization of 'lock_scope' (aka 'unique_ptr *, decltype(&MyClass::free_lock)>') 
     return lock_scope(&lck, &MyClass::free_lock); 
         ^  ~~~~~~~~~~~~~~~~~~~~~~~~~ 
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:130:7: note: candidate constructor not viable: no known conversion from 'std::atomic *' to 'pointer' (aka 'std::atomic **') for 1st argument 
     unique_ptr(pointer __p, 
    ^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:125:7: note: candidate constructor not viable: no known conversion from 'std::atomic *' to 'pointer' (aka 'std::atomic **') for 1st argument 
     unique_ptr(pointer __p, 
    ^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:155:2: note: candidate constructor template not viable: requires single argument '__u', but 2 arguments were provided 
     unique_ptr(unique_ptr&& __u) noexcept 
     ^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:164:2: note: candidate constructor template not viable: requires single argument '__u', but 2 arguments were provided 
     unique_ptr(auto_ptr&& __u) noexcept 
     ^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:114:17: note: candidate constructor not viable: requires 0 arguments, but 2 were provided 
     constexpr unique_ptr() noexcept 
       ^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:120:7: note: candidate constructor not viable: requires single argument '__p', but 2 arguments were provided 
     unique_ptr(pointer __p) noexcept 
    ^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:136:17: note: candidate constructor not viable: requires 1 argument, but 2 were provided 
     constexpr unique_ptr(nullptr_t) noexcept 
       ^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:142:7: note: candidate constructor not viable: requires single argument '__u', but 2 arguments were provided 
     unique_ptr(unique_ptr&& __u) noexcept 
    ^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:262:7: note: candidate constructor not viable: requires 1 argument, but 2 were provided 
     unique_ptr(const unique_ptr&) = delete; 
    ^
1 error generated. 
+2

컴파일하는 데 실패한 코드에 대한 질문을 게시하고 컴파일러 오류 메시지를 포함하지 않는 방법을 이해할 수 없습니다 ... – us2012

답변

0

lock_scope 유형이 std::atomic<int>* (즉 std::atomic<int>**)에 대한 포인터를 래핑하도록 선언되었습니다. free_lock 선언 및 정의에 올바르게 반영되어 있습니다.

그러나 (하나의 간접 참조가 적음) 포인터로 lock_scope을 초기화하려고합니다.

컴파일러 메시지는 오히려 명확하게 진술 :

[...] candidate constructor not viable: 
no known conversion from 'std::atomic *' to 'pointer' (aka 'std::atomic **') [...] 

당신 중 하나가 랩 lck에 대한 추가 간접 필요하거나 덜 한 간접를 사용하는 lock_scopefree_lock을 변경합니다.

+0

감사합니다. – outro56

0
당신은 (..) 반환 lock_scope을 변환 할 수

연타 3.2에 의해보고되었다; std :: move (lock_scope (...));를 반환합니다.

std :: unique_ptr :: operator = (const std :: unique_ptr &)이 비공개이기 때문에;

+0

그 문제가 해결되지 않는다고 생각합니다 – outro56

+0

죄송합니다, 오류를 보지 못했습니다. 그 당시의 메시지. – Kis2012dsh

관련 문제