元の話題からはずれる余談だけれど、静的記憶域期間を持つブロックスコープの変数は最初に通過したときに初期化されるルールがある。 (条件によるので常にではない。)
https://timsong-cpp.github.io/cppwp/n3337/stmt.dcl#4
なのでシングルトンパターンはこう単純化して書くことも出来る。
#include <memory>
class Foo {
Foo() = default;
public:
static Foo* createInstance() {
static std::unique_ptr<Foo> m_pObj = std::unique_ptr<Foo>(new Foo);
return m_pObj.get();
}
};
int main(void) {
auto bar = Foo::createInstance();
}
C++相談室 part165
■ このスレッドは過去ログ倉庫に格納されています
784はちみつ餃子 ◆8X2XSCHEME (ワッチョイ ed32-PAPZ)
2025/03/23(日) 09:32:31.18ID:Ft35v0Bz0■ このスレッドは過去ログ倉庫に格納されています