I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

This is an alternative version. Some would like to call the singleton pattern the anti-pattern. I'm not sure I like the singleton pattern myself.
Sadly, the checks are only done at runtime. I don't know of a way to do it at compile time.

Originally Posted by Adak

io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

Originally Posted by Salem

You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

I've never heard of the Singleton pattern as an anti-pattern. It is in the gang of 4 design patterns book and is a legit pattern. Any pattern can be an anti-pattern if the pattern is used and applied incorrectly.

Some consider it an anti-pattern, judging that it is overused, introduces unnecessary limitations in situations where a sole instance of a class is not actually required, and introduces global state into an application.

Sadly, the checks are only done at runtime. I don't know of a way to do it at compile time.

I think that the check simply cannot be done at compile time because its evaluation depends on the runtime execution.

That means that your suggestion is invalid. It also means that if it is really appropriate to enforce a singleton, your suggestion is probably worse than implementing the singleton pattern.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

I don't believe I agree. The singleton pattern basically creates a global from which you can access anywhere.
My pattern simply makes it impossible to create more than one instance. This forces the object you create to obey scope rules. It also avoids overhead for enforcing the rules (it only asserts in debug mode).
So, they are two different approaches entirely.

Originally Posted by Adak

io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

Originally Posted by Salem

You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

My pattern simply makes it impossible to create more than one instance. This forces the object you create to obey scope rules.

One problem is that the relevant logic error can only be detected at runtime. Another problem is that it may not be obvious, without checking the documentation, that the object is supposed to be a singleton. Things like good naming and disabling copying can help, but it will still not be as obvious. (Come to think about it, it is also unclear how should the destruction of such an object be handled.)

Originally Posted by Elysia

It also avoids overhead for enforcing the rules (it only asserts in debug mode).

This might actually be a disadvantage: if due to an oversight a path by which an object of this type is created twice is somehow not tested, but is used in production, there can be a situation where the singleton requirement is violated. Throwing an exception would avoid this, but since this is fundamentally a programmer's logic error, that would be an unfortunate last resort when it can be avoided in the first place.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.