Assume there is a class Foo
that I, as the designer of a library, do not want my users to be able to instantiate more than n
amount of times (where n
is not necessarily 1
).
Is there any way to enforce this rule during compilation?
Please note that I am not after a Singleton or likewise pattern, as I would like the users to realize they should not instantiate the class, before executing their code.
So far, my best attempt was a combination of static_assert
and the __COUNTER__
macro but to no avail as they do not seem to be evaluated, as I'd expect, inside functions or classes.
constexpr int bar()
{
static_assert( __COUNTER__ < 5, "You called this too many times!");
return 0;
}