Templates 在类中使用模板时出错,之后在函数中使用:没有可行的构造函数或推论

Templates 在类中使用模板时出错,之后在函数中使用:没有可行的构造函数或推论,templates,Templates,我正在制作函数Fold,它可以接受不同的类,例如Sum,它指定了操作类型 #包括 #包括 #包括 模板 结构和{ 公众: 自动运算符()(T init,std::list::const_迭代器优先, std::list::const_迭代器(最后一个){ for(自动它=第一个;它!=最后一个;++它){ init+=*it; } 返回init; } }; 模板 T折叠(先迭代,后迭代,T初始化,运算符func){ 返回函数(初始、第一个、最后一个); } int main(){ std::列

我正在制作函数Fold,它可以接受不同的类,例如Sum,它指定了操作类型


#包括
#包括
#包括
模板
结构和{
公众:
自动运算符()(T init,std::list::const_迭代器优先,
std::list::const_迭代器(最后一个){
for(自动它=第一个;它!=最后一个;++它){
init+=*it;
}
返回init;
}
};
模板
T折叠(先迭代,后迭代,T初始化,运算符func){
返回函数(初始、第一个、最后一个);
}
int main(){
std::列表a{1,2,3,6};

std::cout模板参数是从(函数)参数推导而来的。由于
Sum
没有参数,因此无法推导模板参数。对此进行了解释

但是,所有的都不会丢失。
Sum
作为一个类,不使用模板参数。只有
operator()
方法使用。因此,您可以只将模板放在
operator()
方法中,而将
Sum
作为非模板类

例如:

struct Sum {                                              
public:                                                   
    template <typename T>                
    auto operator() (T init, std::list<int>::const_iterator first,
                    std::list<int>::const_iterator last) {
        for (auto it = first; it != last; ++it) {         
            init += *it;                                  
        }                                                 
        return init;                                      
    }                                                     
};                                                        
struct Sum{
公众:
模板
自动运算符()(T init,std::list::const_迭代器优先,
std::list::const_迭代器(最后一个){
for(auto-it=first;it!=last;++it){
init+=*it;
}                                                 
返回init;
}                                                     
};                                                        
我还将尽可能对迭代器进行模板化,就像您对Fold所做的那样。完整的代码示例以及一些其他使用示例如下:

#include <functional>
#include <iostream>
#include <list>
#include <vector>

struct Sum {
public:
    template <typename T, typename Iterat>
    auto operator() (T init, Iterat first, Iterat last) {
        for (auto it = first; it != last; ++it) {
            init += *it;
        }
        return init;
    }
};

template <typename Iterat, typename T, typename Operator>
 T Fold(Iterat first, Iterat last, T init, Operator func) {
    return func(init, first, last);
}


int main() {
    std::list<int> a{1, 2, 3, 6};
    std::cout << Fold(a.cbegin(), a.cend(), 0, Sum()) << std::endl;
    // init is a float. While the iterators return int.
    std::cout << Fold(a.cbegin(), a.cend(), 0.5 , Sum()) << std::endl;
    std::list<std::string> b{"1", "2", "3", "6"};
    std::cout << Fold(b.cbegin(), b.cend(), std::string(""), Sum()) << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
结构和{
公众:
模板
自动运算符()(T init,先迭代,后迭代){
for(自动它=第一个;它!=最后一个;++它){
init+=*it;
}
返回init;
}
};
模板
T折叠(先迭代,后迭代,T初始化,运算符func){
返回函数(初始、第一个、最后一个);
}
int main(){
std::列表a{1,2,3,6};
标准::cout
struct Sum {                                              
public:                                                   
    template <typename T>                
    auto operator() (T init, std::list<int>::const_iterator first,
                    std::list<int>::const_iterator last) {
        for (auto it = first; it != last; ++it) {         
            init += *it;                                  
        }                                                 
        return init;                                      
    }                                                     
};                                                        
#include <functional>
#include <iostream>
#include <list>
#include <vector>

struct Sum {
public:
    template <typename T, typename Iterat>
    auto operator() (T init, Iterat first, Iterat last) {
        for (auto it = first; it != last; ++it) {
            init += *it;
        }
        return init;
    }
};

template <typename Iterat, typename T, typename Operator>
 T Fold(Iterat first, Iterat last, T init, Operator func) {
    return func(init, first, last);
}


int main() {
    std::list<int> a{1, 2, 3, 6};
    std::cout << Fold(a.cbegin(), a.cend(), 0, Sum()) << std::endl;
    // init is a float. While the iterators return int.
    std::cout << Fold(a.cbegin(), a.cend(), 0.5 , Sum()) << std::endl;
    std::list<std::string> b{"1", "2", "3", "6"};
    std::cout << Fold(b.cbegin(), b.cend(), std::string(""), Sum()) << std::endl;
    return 0;
}