2013-02-16 121 views
1

我有下面的代碼片段,無法編譯...似乎應該,但它現在迴避我。非常感謝任何幫助/建議!錯誤使用靜態函數刪除與std :: unique_ptr

#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); 
} 

}; 

以下錯誤消息報告了鏘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. 
+2

我不明白如何任何人可以發佈有關代碼的問題,無法編譯,並且不包括編譯器錯誤消息... – us2012 2013-02-16 07:14:47

回答

0

您的lock_scope類型被聲明爲包裝指向std::atomic<int>*的指針,即std::atomic<int>**。聲明和定義正確地反映了這一點。

但你嘗試使用指針只是一個std::atomic<int>(一個間接更少)來初始化lock_scope

編譯器的消息指出,相當明確:

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

您也需要一個額外的間接的包裹lck或更改lock_scopefree_lock使用一個間接少。

+0

謝謝你,工作! – outro56 2013-02-16 16:36:48

0

你可以轉換回lock_scope(..);返回std :: move(lock_scope(...));

因爲std :: unique_ptr :: operator =(const std :: unique_ptr &)是私有的;

+0

我不相信解決問題 – outro56 2013-02-16 08:13:50

+0

對不起,我沒有看到錯誤當時的消息。 – Kis2012dsh 2013-02-17 03:07:37

相關問題