I am curious about whether it is possible to ensure at compile time that a method is called in exactly one place.
Note that it is OK if the function is called more than once (e.g. in a loop) - but it should not be called in two separate loops.
This can be broken into two parts, I am also interested in solutions that cover either part:
(a) ensure a method is called in at least one place
(b) ensure a method is called in at most one place
I have full control over the structure of the code, and different idioms that achieve the same idea are welcome.
// class.h
class MyClass {
public:
void my_method();
}
The following should not compile (never called)
#include "class.h"
int main() {
MyClass my_class;
}
The following should not compile (called in more than one place)
#include "class.h"
int main() {
MyClass my_class;
my_class.my_method();
while(true) {
my_class.my_method();
}
}
The following should compile (called in exactly one place):
#include "class.h"
int main() {
MyClass my_class;
while(true) {
my_class.my_method();
}
}