如何限制我的模板仅接受具有特定输入和参数的 lambda输出类型?

发布于 2025-01-10 00:15:37 字数 1354 浏览 2 评论 0原文

受到计算函数泰勒级数的其他问题的启发(原始问题),我编写了一个模板没有任何限制来成功计算总和。这是当前代码(模板主体已删除,正如 @Elliott 所说,这与要点无关..)

#include <iostream>
#include <cmath>
#include <limits>

template<typename ftn>
long double Taylor_sum(ftn terms_to_sum) { /* Summation calculation goes here... */ return result; };

int main(){
    using namespace std; long double x;  cin >> x ;
    long double series_sum = Taylor_sum([x](unsigned long long int i) -> long double { return /*Taylor term here*/; });
    if (!isfinite(series_sum)) cout << "Series does not converge!" << endl;
    else {
        cout << "Series converged, its value is : " << series_sum << endl;
        cout << "Compared to sin                : " << sinl(x) << endl;
    }
}

:自己实践概念,我试图限制模板仅接受以unsigned long long int作为输入的lambda,以及long double作为输出。这是我当前的尝试(无法编译):

template<typename T,integral ARG>
concept my_lambda = requires(T t, ARG u) {
    { return t(u); };
}

template<my_lambda ftn>
long double Taylor_sum(ftn term) { //The rest is same...

我用 google 搜索了各种来源,但在我看来,由于 concept 是 C++20 中相对较新的功能,因此可用的材料似乎较少。有谁知道如何正确限制我的模板参数?

Inspired by other question to calculate taylor series of a function(Original question), I wrote a template without any constraint to successfully calculate the sum. Here is current code (Template body removed, as @Elliott says it's irrelevant to the point..):

#include <iostream>
#include <cmath>
#include <limits>

template<typename ftn>
long double Taylor_sum(ftn terms_to_sum) { /* Summation calculation goes here... */ return result; };

int main(){
    using namespace std; long double x;  cin >> x ;
    long double series_sum = Taylor_sum([x](unsigned long long int i) -> long double { return /*Taylor term here*/; });
    if (!isfinite(series_sum)) cout << "Series does not converge!" << endl;
    else {
        cout << "Series converged, its value is : " << series_sum << endl;
        cout << "Compared to sin                : " << sinl(x) << endl;
    }
}

Although the code works enough, to study & practice the concept myself, I am trying to constrain the template to accept only lambda with unsigned long long int as a input, and long double as output. Here is my current attempt (which does not compile):

template<typename T,integral ARG>
concept my_lambda = requires(T t, ARG u) {
    { return t(u); };
}

template<my_lambda ftn>
long double Taylor_sum(ftn term) { //The rest is same...

I googled various sources, but it seems to me that because the concept is relatively new feature in C++20, there seems less material available. Does anyone know how to constrain my template parameter properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此岸叶落 2025-01-17 00:15:37

我试图限制模板只接受 lambda
unsigned long long int 作为输入,long double 作为输出。

您可以将复合要求return-type-requirement:

template<typename F>
concept my_lambda = requires(F f, unsigned long long int x) {
    { f(x) } -> std::same_as<long double>;
};

template<my_lambda ftn>
long double Taylor_sum(ftn term) { //The rest is same...

演示

I am trying to constrain the template to accept only lambda with
unsigned long long int as a input, and long double as output.

You can use compound requirements with return-type-requirement:

template<typename F>
concept my_lambda = requires(F f, unsigned long long int x) {
    { f(x) } -> std::same_as<long double>;
};

template<my_lambda ftn>
long double Taylor_sum(ftn term) { //The rest is same...

Demo

所谓喜欢 2025-01-17 00:15:37

对我来说,解决方案是 std::convertible_tostd::function 看起来相当简洁:

template<typename T>
concept my_lambda = std::convertible_to<std::function<long double(unsigned long long)>, T>;

template<my_lambda Callback>
long double Taylor_sum(Callback&& term) { /*...*/ }

To me, a solution with std::convertible_to and std::function looks quite concise:

template<typename T>
concept my_lambda = std::convertible_to<std::function<long double(unsigned long long)>, T>;

template<my_lambda Callback>
long double Taylor_sum(Callback&& term) { /*...*/ }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文