开发者

How do you compare two unknown numbers to see if they're equal in a special case template?

Here, is my code. Just trying to wrap my head aro开发者_JS百科und 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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜