-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.cpp
More file actions
38 lines (29 loc) · 797 Bytes
/
singleton.cpp
File metadata and controls
38 lines (29 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (c) December 2025 Félix-Olivier Dumas. All rights reserved.
// Licensed under the terms described in the LICENSE file
template<typename Derived>
struct Singleton {
public:
static Derived& instance() {
static Derived instance(Token{});
return instance;
}
protected:
struct Token {};
Singleton() = default;
private:
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(Singleton&&) = delete;
};
struct World final : Singleton<World> {
friend Singleton<World>;
void foo() const noexcept {}
private:
explicit World(Token) {}
};
int main() {
World::instance().foo(); // ✓
World instance; // ✗
World::foo() // ✗
}