The VC ++ function is called only once, and the vc function is called once.
How to ensure that a function is called only once
One function caller calls another function callee internally. Now, caller may be called multiple times in multiple places, you want callee to be called only once when called for the first time. Generally, callee initializes the environment or resources.
Perhaps, from the code structure perspective, the first thing you think of is to take callee out of caller and put it in a suitable place for initialization. This is a good method, but believe me, in some cases, this is not an effective method: You may not be able to find a "suitable place", or you may find it, but you will lose the benefits of lazy initialization ~~~.
Here, I just want to find a better way to solve this problem.
The first method is simple. It is determined by a static flag:
Static bool flag = false; if (! Flag) {callee (); flag = true ;}
This can work perfectly, but the code feels a little more concise.
In addition, each subsequent call requires an inverse and judgment operation, which affects the performance of frequently called operations. In addition, even if the impact is not big, from the perspective of the programmer's feelings, you do not want to have extra judgment ~~~
Oh, you can remove the reverse:
Static bool flag = true; if (flag) {callee (); flag = false ;}
However, the judgment still exists.
[EDIT: Some people pointed out in the SO discussion that even if static variables are used, there will actually be a judgment operation. This points out the root of the problem, SO the efficiency discussion is unnecessary]
Of course, we also have the second more concise method, assuming that the return type of callee is int:
Static int dummy = callee (); // 1)
After initialization, static variables can be implemented only once, which is simple and efficient.
However, there is a problem: what if the return type of callee is void? You cannot:
Static void dummy = callee (); static int dummy = (int) callee (); static int dummy = reinterpret_cast <int> (callee ());
Because void is not a type, but has no type.
Even if you think you are smart, come up with the following method:
Bool dummyfunc (void) {return true;} static bool dummy = dummyfunc (callee ());
That is not the case. Don't think callee returns void and pass the returned void to the dummyfunc parameter. Because void is not a type and has no value assignment at all, the concept of passing values ~~~
Fortunately, there is a comma expression in c ++. To be honest, C ++ has been using it for more than seven years. This is the first time I found that the comma expression is so cute:
The comma expression calculates Each subexpression and returns the value of the last subexpression.
Therefore, this solution is available:
Static bool dummy = (callee (), true); // 2)
It is also concise and efficient.
It seems that the method of initializing static variables can achieve this goal, and it will be better.
Appendix:
I believe there will be other methods to handle the case where callee returns void. If you know, please don't hesitate to give me some advice!
Source: http://www.cnblogs.com/baiyanhuang/archive/2010/11/13/1876677.html