8,747 questions
2
votes
3
answers
240
views
Is it possible to eliminate the this pointer in a singleton?
This question is about the singleton pattern in modern C++ and one of its limitations in particular.
I can implement the singleton pattern like this:
class Logger
{
public:
static Logger& ...
0
votes
3
answers
255
views
Singleton using Lazy<T> vs Singleton using static field initializer [duplicate]
I am storing some settings for my application in a JSON file located alongside the application .exe. I want to load these settings only once, lazily, the first time one of them is needed. So for ...
Best practices
2
votes
4
replies
129
views
When is it OK to use a singleton?
Follow-up on this question: Singletons: good design or a crutch?
It is true, that using singletons can make testing harder, and bugs can hide behind them. GoF defines singleton to use when:
Global ...
0
votes
0
answers
125
views
What makes QGuiApplication construction hang in this case?
Here's a piece of Qt code:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QGuiApplication app2(argc, argv);
The first constructor call returns, the second does not.
I ...
3
votes
1
answer
91
views
Cannot instantiate singleton class implemented by metaclass with default value
I want to instantiate class with default value.
Class have required parameters.
Class must be singleton (can be implemented with smt other than metaclass).
Data must be updated if try to instantiate ...
0
votes
0
answers
104
views
Use singleton HttpClient in .NET Core for multiple API endpoints calls
According to
[1]: https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines, the recommended approach for handling HTTP requests is to use singleton or static ...
1
vote
2
answers
157
views
How do I test a Singleton for thread-safety in a unit test?
Everyone knows Singletons. Here is one:
public class MySingleton
{
private MySingleton() { }
public static MySingleton GetInstance()
{
return instance ??= new MySingleton();
}
...
0
votes
0
answers
57
views
why does preferencesDataStore() return the same instance? (why a class is a singleton? no need for dagger-hilt?)
This is a Kotlin question, although it originated from Android Jetpack Compose.
I defined the Context.dataStore extension property in the recommended way:
val Context.dataStore: DataStore<...
1
vote
1
answer
78
views
Correct way of resetting a singleton instance in TypeScript
I have a tracker which I'd like to be a singleton class for usage throughout the app. For instance, I came up with the following:
export class MyTracker = {
private static instance: MyTracker | ...
0
votes
0
answers
52
views
Messy call graphs for singleton classes
I'm trying to browse the C++ code for an embedded project using Doxygen 1.8.13.
I find the call/caller graphs very useful for this, but the project includes several singleton classes. Each of these ...
2
votes
3
answers
230
views
How much overhead does access of singleton have?
Does the singleton pattern in C++ have processor overhead? If it does, how much?
static Singleton& getInstance() {
static Singleton instance;
return instance;
}
//...
int ...
2
votes
2
answers
87
views
Forbid multiple service implementations in .NET Generic Host service collection
For .NET Generic Host it is possible to register multiple implementations of same service interface. When materializing that service the last added is obtained (that is correct even for multiple ...
0
votes
2
answers
162
views
Access Services in Class to be used as Singleton
I have a class AppData that I am going to load as Singleton to store data for use across my Blazor web app.
My issue is that the singleton requires some data that is retrieved with asynchronous calls ...
0
votes
1
answer
97
views
Perform an action when all registered services in .NET Generic Host are disposed
I need to perform some action (i. e., log succesfull shutdown) when all registered services in .NET Generic Host are disposed. I hoped IHostApplicationLifetime.ApplicationStopped could help me but it ...
0
votes
1
answer
230
views
Creating a static singleton with an async initializer in rust [duplicate]
I'm working with SurrealDb, and in their docs here they implement a singleton. However, when you try to implement the same thing using a RocksDb instance it requires an asynchronous step making that ...