title: Flexible return type for std::function, when the std::function is an argument of a virtual function
body: <p>My question is related to the question <a href="https://stackoverflow.com/questions/5871722/how-to-achieve-virtual-template-function-in-c">here</a>, but in the context of <code>std::function</code>.</p>
  <p>I have a virtual function that needs to take a <code>std::function</code> as an argument. However, I want the user to be able to choose <code>std::function</code>'s return type in their application code.</p>
  <p>As a concrete example, say we have a base class <code>ProbabilityDistribution</code>, with <code>NormalDistribution</code>, <code>CauchyDistribution</code>, <code>UniformDistribution</code> being its derived classes.</p>
  <p>Suppose a virtual member function <code>compute_weighted_expectation</code> takes a <code>std::function</code> that provides a function to integrate against the given distribution. So if the <code>std::function</code> returns a scalar, we are integrating against a 1-d function. If <code>std::function</code> returns a vector, we are integrating against a set of n 1-d functions and so on.</p>
  <pre><code>class ProbabilityDistribution
  {
  public:
      ProbabilityDistribution() {}
      virtual ~ProbabilityDistribution() {}
  
  private:
  
      template&lt;typename weight_type=float&gt;
      virtual weight_type compute_weighted_expectation(std::function&lt;weight_type(weight_type)&gt; weight_definition) = 0;
  }
  </code></pre>
  <p>If the user wanted to integrate a function f(x) = x^2 against a Normal, they could do,</p>
  <pre><code>NormalDistribution normal_dist(mean, variance);
  std::function&lt;float(float)&gt; square = [](float a) { return a*a; };
  normal_dist.compute_weighted_expectation&lt;float&gt;(square);
  </code></pre>
  <p>If they wanted to integrate vector of functions [f1(x) f2(x) f3(x)] against a Cauchy,</p>
  <pre><code>CauchyDistribution cauchy_dist(location, scale);
  std::function&lt;std::vector&lt;float&gt;(std::vector&lt;float&gt;)&gt; vector_square = [](std:vector&lt;float&gt; a) { return std::transform(a.begin(), a.end(), a.begin(), a.begin(), std::multiplies&lt;float&gt;()); };
  cauchy_dist.weighted_expectation&lt;std::vector&lt;float&gt;&gt;(vector_square);
  </code></pre>
  <p>Now this wont compile, because templates are not allowed around virtual functions. Is there a way I can achieve this functionality without using templates ?</p>
  <p><strong>[EDIT]</strong></p>
  <p>The actual polymorphic class I am dealing with is in the context of a strategy pattern. The integration against probability distributions is the closest analog I could think ok.</p>
  <p>So say that there is a <code>RandomVariable</code> class that takes a generic <code>ProbabilityDistribution</code> object in its constructor. The user selects the specific type of <code>ProbabilityDistribution</code> when they create a <code>RandomVariable</code> object.</p>
  <pre><code>class RandomVariable
  {
  public:
  
  RandomVariable(std::unique_ptr&lt;ProbabilityDistribution&gt; _distribution):rv_distribution(_distribution) {};
  
  ~RandomVariable() {};
  
  private:
  std::unique_ptr&lt;ProbabilityDistribution&gt; rv_distribution;
  
  float get_expectation() {return rv_distribution-&gt;compute_weighted_expectation(std::function&lt;float(float)&gt; _identity); };
  }
  </code></pre>
  <p>User invocation:</p>
  <pre><code>NormalDistribution normal_dist(mean, variance);
  RandomVariable rv_1(normal_dist);
  rv_1.get_expectation();
  </code></pre>