Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何实现模板函数的容器和迭代器的重载?_C++_Templates_Overloading - Fatal编程技术网

C++ 如何实现模板函数的容器和迭代器的重载?

C++ 如何实现模板函数的容器和迭代器的重载?,c++,templates,overloading,C++,Templates,Overloading,我想要一个模板函数的4个重载。该函数取一个值范围y和另一个值范围x。这些范围可以指定为迭代器或容器。如果作为容器传递,则使用begin/end为容器调用迭代器函数。对于x,存在一种特殊情况,其中传递标量x将被视为[开始,结束]范围内的所有x都是该值 #包括 #包括 //1)取迭代器[begin,end]作为y值,取标量作为x值 模板 向量 func(迭代器ybegin,迭代器yend,typename迭代器::value_type x=1。){} //2)取y值序列和x的标量 模板 向量 fun

我想要一个模板函数的4个重载。该函数取一个值范围y和另一个值范围x。这些范围可以指定为迭代器或容器。如果作为容器传递,则使用
begin
/
end
为容器调用迭代器函数。对于x,存在一种特殊情况,其中传递标量x将被视为[开始,结束]范围内的所有x都是该值

#包括
#包括
//1)取迭代器[begin,end]作为y值,取标量作为x值
模板
向量
func(迭代器ybegin,迭代器yend,typename迭代器::value_type x=1。){}
//2)取y值序列和x的标量
模板
向量
func(常量序列&y,类型名称序列::值\类型x=1。){
返回函数(y.begin(),y.end(),x);
}
//3)取迭代器[begin,end]表示y值,取迭代器[begin,end]表示x值
模板
向量
func(迭代器1 ybegin,迭代器1 yend,迭代器2 xbegin,迭代器2 xend){}
//4)取y值顺序和x值顺序
模板
向量
func(常数序列1和y、常数序列2和x){
返回函数(y.begin(),y.end(),x.begin(),x.end());
}
int main(){
std::向量a{4,5,6};
std::向量b{1,1,1};
func(a.begin(),a.end(),0.2);
func(a.begin(),a.end(),b.begin(),b.end());
func(a,0.2);
func(a,b);
}
我上面发布的代码没有编译,因为带有
std::vector
double
的调用适合函数2)(正确)和4)(不正确)

错误:重载“func(std::vector&,double)”的调用不明确
func(a,0.2);
^
scratch_1.cpp:33:44:注:候选:“std::vector func(const Sequence&,typename Sequence::value\u type)[带Sequence=std::vector;typename Sequence::value\u type=double]”
std::vector func(常量序列&y,
^~~~
scratch_1.cpp:50:21:注:候选:“std::vector func(const Sequence1&,const Sequence2&)[带Sequence1=std::vector;Sequence2=double]”
标准::向量函数(常数序列1和y,常数序列2和x){
如果我使用以下版本的2)和4),我会

/2)
模板
向量
func(常量序列&y,类型名称std::enable_if::type x=1){
返回函数(y.begin(),y.end(),x);
}
// 4)
模板
向量
函数(常数序列1和y),
const typename std::enable_if::value,Sequence2>::type&x){
返回函数(y.begin(),y.end(),x.begin(),x.end());
}
错误:调用'func(std::vector&,std::vector&')时没有匹配的函数。
因为无法推断第二个向量的类型

 error: no matching function for call to ‘func(std::vector<double>&, std::vector<int>&)’
     func(a, b);

candidate: ‘template<class Sequence1, class Sequence2> std::vector<double> func(const Sequence1&, const typename std::enable_if<(! std::is_scalar<Sequence2>::value), Sequence2>::type&)’
 std::vector<double> func(const Sequence1& y, const typename
                     ^~~~
note:   template argument deduction/substitution failed:
note:   couldn't deduce template parameter ‘Sequence2’ func(a, b);
错误:调用“func(std::vector&,std::vector&)”时没有匹配的函数
func(a,b);
候选:“模板std::vector func(常量Sequence1&,常量typename std::enable_if::type&)”
标准::向量函数(常量序列1&y,常量类型名
^~~~
注意:模板参数扣除/替换失败:
注意:无法推断模板参数'Sequence2'func(a,b);
使用我明目张胆偷来的:

// 2) take sequence of y values and scalar for x
template <typename Sequence, 
        std::enable_if_t<is_container<Sequence>::value, int> =0
>
std::vector<typename Sequence::value_type> 
func(const Sequence& y, typename Sequence::value_type x = 1.) {
    return func(y.begin(), y.end(), x);
}

// 4) take sequence of y values and sequence of x values
template <typename Sequence1, typename Sequence2, 
        std::enable_if_t<is_container<Sequence1>::value, int> =0, 
        std::enable_if_t<is_container<Sequence2>::value, int> =0 
>
std::vector<double> 
func(const Sequence1& y, const Sequence2& x) {
    return func(y.begin(), y.end(), x.begin(), x.end());
}
//2)取y值序列和x的标量
模板
向量
func(常量序列&y,类型名称序列::值\类型x=1。){
返回函数(y.begin(),y.end(),x);
}
//4)取y值顺序和x值顺序
模板
向量
func(常数序列1和y、常数序列2和x){
返回函数(y.begin(),y.end(),x.begin(),x.end());
}

使用我明目张胆偷来的:

// 2) take sequence of y values and scalar for x
template <typename Sequence, 
        std::enable_if_t<is_container<Sequence>::value, int> =0
>
std::vector<typename Sequence::value_type> 
func(const Sequence& y, typename Sequence::value_type x = 1.) {
    return func(y.begin(), y.end(), x);
}

// 4) take sequence of y values and sequence of x values
template <typename Sequence1, typename Sequence2, 
        std::enable_if_t<is_container<Sequence1>::value, int> =0, 
        std::enable_if_t<is_container<Sequence2>::value, int> =0 
>
std::vector<double> 
func(const Sequence1& y, const Sequence2& x) {
    return func(y.begin(), y.end(), x.begin(), x.end());
}
//2)取y值序列和x的标量
模板
向量
func(常量序列&y,类型名称序列::值\类型x=1。){
返回函数(y.begin(),y.end(),x);
}
//4)取y值顺序和x值顺序
模板
向量
func(常数序列1和y、常数序列2和x){
返回函数(y.begin(),y.end(),x.begin(),x.end());
}