Is there a more efficient way to structure/pattern C++ than this?
<p>The basic idea is that I have a "family" of classes that all do the same identical thing, but in a slightly different way.    This "family" is used in a "high performance" loop, so speed is of the essence.    Further, the specific family is specified by a configuration file...</p>

<p>The problem is that there is tremendous code repetition in the main function here.  Is there a better way to structure this so I don't have to write <code>HP&lt;objx&gt; test</code> and <code>test.loop(bobloblaw)</code> three times?  (In practice, this chunk of code is many more lines than 2...)</p>

<pre><code>class obj1 {
public:
    double f(double x) const { return 1.; }
};

class obj2 {
public:
    double f(double x) const { return x; }
};

class obj3 {
public:
    double f(double x) const { return x*x; }
};

template &lt;class O&gt;
class HP {
private:
    O obj;

public:

    double loop(const vector&lt;double&gt;&amp; x) {
        double s = 0.;
        for (auto i : x) s += obj.f(i);

    return s;
    }
};

int main() {
    string config = "bob";
    double result = 0;
    vector&lt;double&gt; bobloblaw;

    /* Read configuration file to determine which object to use. */
    if (config == "obj1") {
        HP&lt;obj1&gt; test;
        result = test.loop(bobloblaw);
    } else if (config == "obj2") {
        HP&lt;obj2&gt; test;
        result = test.loop(bobloblaw);
    } else if (config == "obj3") {
        HP&lt;obj3&gt; test;
        result = test.loop(bobloblaw);
    }

    return result;
}
</code></pre>
