title: c++11 : generic type push via a std::function<T(const U&)>
body: <p>I am trying to build a generic push component.</p>
  
  <p>I have a class <code>store&lt;T&gt;</code> which </p>
  
  <ul>
  <li>has a T t; member</li>
  <li>has a <code>void push(const T &amp; t)</code> method called by data providers.</li>
  </ul>
  
  <p>When push is called by a provider, I want a value computed by a <code>std::function&lt; T2(const T&amp;)&gt;</code>  and all clients (<code>store&lt;T2&gt;</code>) are notified
  with that result value of type T2. A store client has first to subscribe to store via a <code>linker&lt;T, T2&gt;</code> object.</p>
  
  <pre><code>template &lt;class data&gt; class store
  {
      data data_;
      std::list&lt; action&lt;data &gt; &gt; m_links;
  public:
      void push(const data &amp; e)
      {
          data_ = e;
          for(action&lt;data&gt; a : m_links)
              a(data_);
      }
  
      void subscribe(action&lt;data&gt; d)
      {
          m_links.push_back(d);
      }
  };
  </code></pre>
  
  <p>Linker object :</p>
  
  <pre><code>template &lt; class data1, class data2 &gt; class linker
  {
      // source where come the push calls
      store&lt;data1&gt; &amp; m_source;
      // targets to be notified after computation
      std::list&lt;store&lt;data2&gt; * &gt; m_targets;
      // computation function
      std::function&lt; data2(const data1 &amp;)&gt; m_func;
  
  public:
      linker(store&lt;data1&gt; &amp; source, std::function&lt; data2(const data1 &amp;)&gt; func)
      : m_source(source), m_func(func)
      {
          m_source.subscribe([this](const data1 &amp; d){this-&gt;push(d);});
      }
      // add a client
      void add_target(store&lt;data2&gt; &amp; target)
      {
          m_targets.push_back(&amp;target);
      }
      void push(const data1 &amp; e)
      {
          //compute result
          data2 d2 = m_func(e);
          // notify all
          for(store&lt;data2&gt; * s2 : m_targets)
          {
              s2-&gt;push(d2);
          }
      }
  };
  </code></pre>
  
  <p>Use case :</p>
  
  <pre><code>int main()
  {
      // function : just increment an int, return as double
      std::function&lt;double(const int &amp;) &gt; f_inc = [](const int&amp; i){ return i+1;};
      // function : display the data 
      std::function&lt;int(const double&amp;) &gt; f_display = [](const double&amp; d){ std::cout &lt;&lt; "value=" &lt;&lt; d &lt;&lt; std::endl ; return 0;};
  
      store&lt;int&gt; source;
      store&lt;double&gt; target, target2;
  
      linker&lt;int, double&gt; l(source, f_inc);
      l.add_target(target);
      l.add_target(target2);
  
  
      linker&lt;double, int&gt; display(target, f_display);
  
      source.push(1);
  
      return 0;
  }
  </code></pre>
  
  <p>I want to suppress the explicit-ness of the 'linker' object. I did not succeed because I dont know how to handle the fact that when a store client subscribes to a store object, the object can not store a pointeur to store since it does not know the type T2 !  </p>
  
  <p>I would like to write something like that:</p>
  
  <pre><code>std::function&lt;double(const int &amp;) &gt; f_inc = [](const int&amp; i){ return i+1;};
  store&lt;int&gt; source;
  store&lt;double&gt; target;
  
  source.link_to(target, f_inc);
  </code></pre>
  
  <p>and be able to unsubscribe :</p>
  
  <pre><code>source.unlink(target, f_inc);
  </code></pre>
  
  <p>or with ids:</p>
  
  <pre><code>id i = source.link_to(target, f_inc);
  source.unsubscribe(i);
  </code></pre>
  
  <p>I am using codeblocks + mingw 4.8.1 under windows xp.
  I guess a design pattern exists for this use case ...</p>
  
  <p>ps: I cant use boost.</p>