F-bounded polymorphism is sometimes called "backwards polymorphism" or the "Curiously recurring template pattern". It can be used to accomplish something to this effect.
Interesting conception.
I used it except I still have a incremental function in the base class only:
staticvoid AddInstance() { counter++; }
So far it is working pretty well. What I was looking for.
I just want to sure that it does not differ from that you posted ( using this pointer inside each class ) and in future I won't meet some surprising errors or unwanted behavior.
According to wiki it is allowed to do so.
But if I chose to increment counter inside the constructor of each class would I get b1?
No - I only wrote it that way for brevity, which I see now was at the expense of clarity (my apologies)!
Inheritance models an is-a relationship, so given that C inherits from B, if any C is created, a B is created too, and the counter reflects that.
C does indeed has a separate counter inherited from Protocol<C>, but you'd need to refer to that using a qualified name (i.e., Protocol<C>::counter), because B::counter is found first.
If you wanted to avoid adding one to the counter for B when constructing a C, you'd need to increment only the appropriate counter in the constructor of B and C, which mostly defeats the point of this technique. But I'm not convinced that this is the correct behavior.