error using static function deleter with std::unique
I have the following code snippet that fails to compile...seems like it should, but it evading me right now. Greatly appreciate any help/suggestions!
#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);
}
};
The following error message was reported by Clang 3.2
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.
Your lock_scope
type is declared to wrap a pointer to std::atomic<int>*
, ie a std::atomic<int>**
. The free_lock
declaration and definition correctly reflects that.
But then you attempt to initialize the lock_scope
with a pointer to just a std::atomic<int>
(one indirection less).
The compiler messages state that rather clearly:
[...] candidate constructor not viable:
no known conversion from 'std::atomic *' to 'pointer' (aka 'std::atomic **') [...]
You either need an additional indirection for the wrapped lck
or change lock_scope
and free_lock
to use one indirection less.
You could convert return lock_scope(..); to return std::move( lock_scope(...) );
Because std::unique_ptr::operator = (const std::unique_ptr& ) is private ;
链接地址: http://www.djcxy.com/p/66738.html