この質問How to pass template function with default arguments to std::call_onceに加えて、関数ポインタを使用するときにデフォルトの引数を渡して解決されましたが、実際のコードでこの解決策を試したところ、 STDにオーバーロード関数を扱う場合 std :: call_onceでオーバーロードされた関数を使用する方法
std::once_flag flag;
class LifeTrackerHelper
{
public:
template<class T>
inline static int SetLongevity(std::unique_ptr<T>& pobj,unsigned int longevity = 0)
{
return 0;
}
template<class T>
inline static int SetLongevity(unsigned int longevity = 0)
{
return 0;
}
};
template<class T>
class Singleton
{
public:
inline static T* getInstance()
{
static std::unique_ptr<T> ptr(new T());
std::call_once(flag,&LifeTrackerHelper::SetLongevity<T>,std::ref(ptr),0);
//static int i = LifeTrackerHelper::SetLongevity<T>(ptr);
// if call_once is commented and above line uncommented this will work
return ptr.get();
}
};
class Test
{
public:
void fun()
{
std::cout<<"Having fun...."<<std::endl;
}
};
;
int main()
{
Singleton<Test>::getInstance()->fun();
return 0;
}
だから、特別なルールがある::なcall_once
ラムダを 'std :: call_once'パラメータとして使うのはなぜですか? – WhiZTiM
'std :: call_once(フラグ、[&ptr] {LifeTrackerHelper :: SetLongevity(ptr、0);}); ' –