title: Can I enforce this operator call count requirement at compile time?
body: <p>I have a class which, interface-wise, is as simple as this:</p>
  
  <pre><code>struct Foo
  {
      inline Foo &amp; operator &lt;&lt; (int i)
      {
          return *this;
      }
  };
  </code></pre>
  
  <p>I can then use it in the following way:</p>
  
  <pre><code>Foo foo;
  foo &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3 &lt;&lt; 4;
  </code></pre>
  
  <p>Now I would like to restrict the usage of this operator. <strong>For instance</strong>, I would like it to be called an even number of times between sequence points.</p>
  
  <p>I currently address this problem using an internal proxy class. A temporary is created which, at the end of the control sequence, is destroyed and checks how many times the operator has been called:</p>
  
  <pre><code>struct Foo
  {
      inline Foo() : m_count(0) {}
  
  private:
      struct FooProxy
      {
          friend struct Foo;
  
          inline ~FooProxy();
          inline struct Foo &amp; operator &lt;&lt; (int i);
  
      private:
          inline FooProxy(struct Foo &amp;foo) : m_foo(foo) {}
          struct Foo &amp;m_foo;
      };
  
  public:
      inline FooProxy operator &lt;&lt; (int i);
  
  private:
      int m_count;
  };
  
  inline Foo::FooProxy Foo::operator &lt;&lt; (int i)
  {
      ++m_count;
      return FooProxy(*this);
  }
  
  inline Foo &amp; Foo::FooProxy::operator &lt;&lt; (int i)
  {
      ++m_foo.m_count;
      return m_foo;
  }
  
  inline Foo::FooProxy::~FooProxy()
  {
      assert(m_foo.m_count % 2 == 0);
  }
  </code></pre>
  
  <p>There are a few caveats but it mostly does the job:</p>
  
  <pre><code>Foo foo;
  foo &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3 &lt;&lt; 4; /* is OK */
  foo &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3; /* triggers an assert */
  </code></pre>
  
  <p>Now I am wondering whether there is a way to enforce this at compile time, either using the same proxy technique, or using another strategy.</p>
  
  <p>Another example of what I would like to achieve: enforce pushing at least one <code>int</code> after any number of <code>float</code> have been passed to the operator:</p>
  
  <pre><code>foo &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3.f &lt;&lt; 4.f &lt;&lt; 5; /* is OK */
  foo &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3.f &lt;&lt; 4.f; /* illegal because one `int` is needed */
  </code></pre>