My first (simple) template that does more than storing a value of type T I just decided to get a bit more acustomed to using templates. Maybe this is a stupid question, but in my experience in C++ there is always some pitfall lurking aroung the corner. Thus I prefer the danger of embarresment instead of building up on bad habbits. Mainly as an exercise I wrote this:

#include &lt;functional&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

template &lt;typename V,template &lt;class&gt; class R = std::greater,typename I=void&gt;
struct Extremum {
    typedef V    value_type;
    typedef I    index_type;
    typedef R&lt;V&gt; relation_type;
    V value;
    I index;
    R&lt;V&gt; r;
    bool operator() (V v,I i){
        bool result = r(v,value);
        if (result){
            value = v;
            index = i;
        }
        return result;
    }
    Extremum(V v,I i) : value(v),index(i) {}
    std::string toString(){
        std::stringstream s;
        s &lt;&lt; "value= " &lt;&lt; value &lt;&lt; " index= " &lt;&lt; index;
        return s.str();
    }
};

template &lt;typename V,template &lt;class&gt; class R&gt;
struct Extremum&lt;V,R,void&gt; {
    typedef V    value_type;
    typedef void index_type;
    typedef R&lt;V&gt; realtion_type;
    V value;
    R&lt;V&gt; r;
    bool operator() (V v){
        bool result = r(v,value);
        if (result){ value = v; }
        return result;
    }
    Extremum(V v) : value(v) {}
    std::string toString(){
        std::stringstream s;
        s &lt;&lt; "value= " &lt;&lt; value;
        return s.str();
    }
};


which would be used like this:

Extremum&lt;int,std::greater,int &gt; e(0,0);
e(10,3);
e(123,5);
std::cout &lt;&lt; e.toString() &lt;&lt; std::endl;

Extremum&lt;int&gt; g(0);
g(123);
g(1234);
g(1);
std::cout &lt;&lt; g.toString() &lt;&lt; std::endl;


I used a ´toString()´ because it is for debugging and printing to the screen while I might want to overload in/output operators for real in/output.

I dont like so much the ordering of the template parameters, as both the second and the third parameter would make a good fit as the last parameter so it can be ommited on the instantiation. However, I guess there is no better solution than simply choosing one ordering.

I have no idea how to provide good initial values. My idea was to use something like std::numeric_limits&lt;V&gt;::max, however, it depends too much on the choosen relation whether it should be max, min or something completely different. Maybe the easiest is  let the user choose them when calling the constructor.

Aside from its functionaliy, which probably could be achieved with less code and a simple lambda or the like, is there anything inherently broken with this code? Anything obvious that could be improved?

Note that this is pre-C++11, but I would also be interested in any post-C++11 stuff.
Apart from cosmetic details, I see two things that could be improved IMHO.

The template comparator R&lt;V&gt;

As a template argument, you are asking for template &lt;class&gt; class R
which is then used so R&lt;V&gt; r;

This narrows the possibilities since you can't pass a non-template comparator.
For instance, if you have a custom type MyType and a custom comparator for that type MyTypeComp then you won't be able to use it with your Extremum struct because MyTypeComp doesn't accept template arguments. 

The fix is easy:

template &lt;
          typename V,
          class R,
          typename I = void
         &gt;
struct Extremum {
    typedef V    value_type;
    typedef I    index_type;
    typedef R relation_type;
    V value;
    I index;
    R r;
    //...


And you would use it so:

Extremum&lt;int, std::greater&lt;int&gt;, int&gt; e(0,0);
Extremum&lt;MyType, MyTypeComp, int&gt; e(MyZero,0);


The index

You write almost the same code twice: once with index, once without.
You could avoid it by passing the index in a pair.
So you have only one class Extremum&lt;V, R&gt; and you set V = std::pair&lt;Value, Index&gt; and an appropriate comparator that compares two pairs according to their Value entry only.

template &lt;class V, class R&gt;
struct Extremum {
    typedef V    value_type;
    typedef R    relation_type;
    V value;
    R r;
    bool operator() (V v){
        bool result = r(v,value);
        if (result) {
            value = v; //you can use std::move here
        }
        return result;
    }
    Extremum(V v, R r) : value{v}, r{r} {} //you can use std::move here too
    std::string toString() const {
        std::stringstream s;
        s &lt;&lt; "value= " &lt;&lt; value;
        return s.str();
    }
};

template &lt;class RV&gt;
struct CompByFirstEntry {
    RV delegate;
    CompByFirstEntry(RV delegate);

    template &lt;class V, class I&gt;
    bool operator() (std::pair&lt;V,I&gt; const&amp; lhs, std::pair&lt;V,I&gt; const&amp; rhs) const {
        return delegate(lhs.first, rhs.first);
    }

    //or, even:

    template &lt;class Pair&gt;
    bool operator() (Pair const&amp; lhs, Pair const&amp; rhs) const {
        return delegate(lhs.first, rhs.first);
    }
} 




EDIT : Here is a working example.


What changed: define the constructor CompByFirstEntry::CompByFirstEntry and operator&lt;&lt; for std::pair; choose one version of the operator().
Note: you can also create a class ExtremumIndexed&lt;V, I, R&gt; that extends Extremum&lt;std::pair&lt;V,I&gt;, MyPairComp&lt;R&gt;&gt; or simply a template using directive.




std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, std::pair&lt;int, int&gt; const&amp; p)
{
    out &lt;&lt; p.first &lt;&lt; " " &lt;&lt; p.second;
    return out;
}

template &lt;class V, class R&gt;
struct Extremum {
    typedef V    value_type;
    typedef R    relation_type;
    V value;
    R r;
    bool operator() (V v){
        bool result = r(v,value);
        if (result) {
            value = v; //you can use std::move here
        }
        return result;
    }
    Extremum(V v, R r) : value{v}, r{r} {} //you can use std::move here too
    std::string toString() const {
        std::stringstream s;
        s &lt;&lt; "value = " &lt;&lt; value;
        return s.str();
    }
};

template &lt;class RV&gt;
struct CompByFirstEntry {
    RV delegate;
    CompByFirstEntry(RV delegate) : delegate{delegate} {}

    template &lt;class V, class I&gt;
    bool operator() (std::pair&lt;V,I&gt; const&amp; lhs, std::pair&lt;V,I&gt; const&amp; rhs) const {
        return delegate(lhs.first, rhs.first);
    }
};

int main(int argc, const char * argv[]) {
    using MyPairComp = CompByFirstEntry&lt;std::greater&lt;int&gt;&gt;;
    MyPairComp comp(std::greater&lt;int&gt;{});
    int value = 1;
    int index = 0;

    Extremum&lt;std::pair&lt;int,int&gt;, MyPairComp&gt; e({value, index}, comp);
    std::cout &lt;&lt; e.toString() &lt;&lt; std::endl;

    e({value + 1, index - 1});
    std::cout &lt;&lt; e.toString() &lt;&lt; std::endl;

    return 0;
}