我一直試圖將以下類的一個靜態方法作為deleter的unique_ptr來傳遞,但我無法弄清楚正確的語法。
如果下面的語法有很大的錯誤,請原諒,因為我剛開始接觸unique_ptr。
#include <iostream>
#include < memory>
class Singleton {
public:
static void deleter(Singleton* p){
delete p。
std::cout << "In deleter
" << std::endl;
}
static std::unique_ptr<Singleton, decltype(& Singleton: :deleter)>&getInstance(void)>。
void hello (void){
std::cout << "Hello!
" << std::endl;
}
Singleton(const Singleton&) = delete;
Singleton(Singleton& &) = delete;
Singleton& operator=(const Singleton& ) = delete;
Singleton& operator=(Singleton& &) = delete;
private:
static std::unique_ptr<Singleton, decltype(&Singleton::deleter)> s_instance_;
Singleton() { std::cout << "constructed
" << std::endl; } ~"Constructed
~Singleton() { std::cout << "Destroyed
" << std::endl; }; { std::cout < "Destroyed
};
std::unique_ptr<Singleton, decltype(& Singleton::delete)>&
Singleton::getInstance (void)。
{
if (s_instance_ == nullptr) {
s_instance_ = std::unique_ptr<Singleton, decltype(&Singleton: :deleter)>(new Singleton(), &Singleton::deleter) 。
}
return s_instance_;
}
std::unique_ptr<Singleton, decltype(&Singleton::deleter)> Singleton::s_instance_{nullptr};
int main ()
{
bool some_condition = true;
std::unique_ptr<Singleton, decltype(&Singleton::deleter)> &ins = Singleton::getInstance();
ins->hello()。
if (some_condition) {
ins.reset()。
}
std::cout << "End of main
" << std::endl;
return 0;
這在使用g 時產生了以下錯誤(--版本==g (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609)
在從/usr/include/c /5/memory:81:0,
從uptr.cpp:2。
/usr/include/c /5/bits/unique_ptr.h。在'constexpr std::unique_ptr<_Tp, _Dp>::unique_ptr() [with _Tp = Singleton; _Dp = void (*)(Singleton*) ] 的實體化。
/usr/include/c /5/bits/unique_ptr. h:200:61: required from 'constexpr std::unique_ptr<_Tp, _Dp>::unique_ptr(std:: nullptr_t) [with _Tp = Singleton; _Dp = void (*)(Singleton*); std::nullptr_t = std::nullptr_t]'
uptr.cpp:44:89: 要求從這里開始
/usr/include/c /5/bits/unique_ptr.h:159:9: 錯誤。static斷言失敗:用空函式指標洗掉器構建
{ static_assert(!is_pointer<deleter_type>:value,
^
另外,我有沒有辦法縮短std::unique_ptr<Singleton, decltype(&Singleton::deleter)>?我試著在類定義前使用以下方法,但編譯器因型別不完整而出錯:
using SingletonUPtr = std::unique_ptr<Singleton, decltype(&Singleton::deleter)> 。
uj5u.com熱心網友回復:
錯誤出現在第44行,這似乎是對Singleton::s_instance_變數的定義。你沒有在那里提供洗掉器函式。你應該在定義中包含它,就像你在前面的getInstance中做的那樣。
std::unique_ptr<Singleton, decltype(&Singleton::deleter)> Singleton::s_instance_{nullptr,&Singleton::deleter};
uj5u.com熱心網友回復:
沒有必要同時存盤型別和值。 型別可以做調度作業。
在c 11,你可以直接寫這個玩具:
template<class F,F f>
struct call_t {
constexpr operator F() const{ return f。}
};
它神奇地發揮作用。 (當你用()呼叫它時,C 會尋找一個轉換為函式的指標)。) 這是std::integral_constant的部分實作,具有c 11中缺少的特征。 在這種情況下,我把它作為一個呼叫f型別的常數評估值F的型別。 它還有其他(類似的)用途。
試一試:
在這個例子中,我是把它作為呼叫F型別的常數評估值的型別。
試試這個:
void foo(){ std: :cout << "hello world
"; }
int main() {
using foo_t = call_t< void(*)(), foo > 。
foo_t foo_v = {};
foo_v(); //hello world。
foo_t{}(); //hello world。
std::cout << sizeof(foo_t) << "
"; //1, 物件是無狀態的。
std::cout << sizeof(&foo) << "
"; //4或8,指標有狀態。
}
剩下的部分看起來像:
class Singleton {
public:
static void deleter(Singleton* p){
delete p。
std::cout << "In deleter
" << std::endl;
}
using upSingleton = std::unique_ptr<Singleton, call_t<decltype(& Singleton::deleteter), & Singleton::deleteter> > 。
static upSingleton& getInstance(void);
void hello() {
std::cout << "Hello!
" << std::endl;
}
Singleton(const Singleton&) = delete;
Singleton(Singleton& &) = delete;
Singleton& operator=(const Singleton& ) = delete;
Singleton& operator=(Singleton& &) = delete;
private:
static upSingleton s_instance_;
Singleton() { std::cout << "constructed
" << std::endl; } ~"Constructed
~Singleton() { std::cout << "Destroyed
" << std::endl; }; { std::cout < "Destroyed
};
Singleton::upSingleton&
Singleton::getInstance()
{
if (s_instance_ == nullptr) {
s_instance_ = upSingleton(new Singleton()) 。
}
return s_instance_;
}
Singleton::upSingleton Singleton::s_instance_{nullptr}。
int main()
{
bool some_condition = true;
Singleton::upSingleton &ins = Singleton::getInstance()。
ins->hello()。
if (some_condition) {
ins.reset()。
}
std::cout << "End of main
" << std::endl;
return 0;
實時示例.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/314108.html
標籤:
上一篇:使用make命令編譯C 11
