本文主要是介绍模板的参数,必须给全。不管是隐式还是显式还是默认值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
注意,虽然在函数调用时只用到了第一个参数类型,但第二个还是要给出来。
不管是显式,还是以默认值的形式。
#include <iostream>struct XYZ
{int begin{1};int end{1};
};template <typename Sequence, typename M >void test452(Sequence x)
{std::cout << "1" << std::endl;
}int main()
{XYZ o;test452< XYZ, XYZ>(o);
}
如果写成这样:
#include <iostream>struct XYZ
{int begin{1};int end{1};
};template <typename Sequence, typename M >void test452(Sequence x)
{std::cout << "1" << std::endl;
}int main()
{XYZ o;test452< XYZ /*,XYZ*/>(o); //编译失败
}error C2672: “test452”: 未找到匹配的重载函数
error C2783: “void test452(Sequence)”: 未能为“M”推导 模板 参数
note: 参见“test452”的声明
当然,也可以给出默认值:
#include <iostream>struct XYZ
{int begin{1};int end{1};
};template <typename Sequence, typename M = int> /*指定默认值*/void test452(Sequence x)
{std::cout << "1" << std::endl;
}int main()
{XYZ o;test452< XYZ /*,XYZ*/>(o); //编译通过
}
这篇关于模板的参数,必须给全。不管是隐式还是显式还是默认值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!