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

How to constrain my template to only accept lambda with specific input & output type?

受其他问题计算函数的泰勒级数(Original question)的启发,我写了一个模板,没有任何限制,成功计算了总和。这是当前代码(删除了模板主体,因为@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;
    }
}

虽然代码足够有效,但为了学习和练习 concept 我自己,我试图限制模板只接受带有 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...

我用谷歌搜索了各种资源,但在我看来,因为 concept 在 C++20 中是相对较新的功能,所以可用的 material 似乎更少。有谁知道如何正确约束我的模板参数?

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

您可以将 compound requirementsreturn-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