views:

55

answers:

1

Here, is my code. Just trying to wrap my head around some of the basic things you can do with TMP. I'm trying to supply two numbers with which the compiler will add up that range of numbers. I'm just not sure how to write the syntax for the "constraint" template.

template < int b, int e >
struct add {
    enum { sum = add< b + 1, e >::sum + b };
};

template <>
struct add< e, e > {
    enum { sum = 0 };
};

int main() {
    cout << add< 4, 8 >::sum << endl;   //30
    return 0;
}
+4  A: 
template <int e>
struct add< e, e > { ...

And the result is 4 + 5 + 6 + 7 + 0 == 22, not 4 + 5 + 6 + 7 + 8 == 30. Once e==e in add<...>, add<...>::sum==0, not e.

MSN
heh thanks. Out of all the tuts I've seen so far, none of em had something like that for the special case template contraint. Didn't even occur to me, bleh
Justen
Oh, and I noticed that, I changed the sum = 0 to sum = e for the special case.
Justen