gpt4 book ai didi

c++ - 您如何比较两个未知数以查看它们在特例模板中是否相等?

转载 作者:行者123 更新时间:2023-11-27 23:33:51 25 4
gpt4 key购买 nike

这是我的代码。只是想了解一下您可以使用 TMP 做的一些基本事情。我正在尝试提供两个数字,编译器会将这两个数字相加到该数字范围内。我只是不确定如何编写“约束”模板的语法。

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 > { ...

结果是4 + 5 + 6 + 7 + 0 == 22 , 不是 4 + 5 + 6 + 7 + 8 == 30 .一次e==eadd<...> , add<...>::sum==0 , 不是 e .

关于c++ - 您如何比较两个未知数以查看它们在特例模板中是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2640101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com