C++ 如何重载模板结构?
我想写一个模板结构,这样可以接受2或3个类型名。但是,程序会产生一个错误,C++ 如何重载模板结构?,c++,templates,struct,overloading,C++,Templates,Struct,Overloading,我想写一个模板结构,这样可以接受2或3个类型名。但是,程序会产生一个错误,template…”不能重载。如何纠正这一点 template< typename F1, typename F2, typename F3> // this shouldn't be right because the compiler expects three typenames, and the program can provide two struct Force{ F1 force
template…”不能重载。如何纠正这一点
template< typename F1, typename F2, typename F3> // this shouldn't be right because the compiler expects three typenames, and the program can provide two
struct Force{
F1 force1;
F2 force2;
F3 force3;
Force(F1 f1, F2 f2) : force1(f1), force2(f2) { // construct out of two forces
}
Force(F1 f1, F2 f2, F3 f3) : force1(f1), force2(f2), force3(f3) { // construct out of three forces
}
Point operator()(double t) {
return force1(t) + force2(t);
}
Point operator()(double t) { // this overloading should not be right because it has the same signature as above
return force1(t) + force2(t) + force3(t);
}
};
// this is used by the main program
template< typename F1, typename F2>
Force<F1, F2> make_physics(F1 first, F2 second){
return Force<F1, F2>(first, second);
}
// this is used by the main program
template< typename F1, typename F2, typename F3>
Force<F1, F2, F3> make_physics(F1 first, F2 second, F3 third){
return Force<F1, F2, F3>(first, second, third);
}
template//这不应该正确,因为编译器需要三个typename,而程序可以提供两个
结构力{
F1力1;
F2力2;
F3力3;
力(F1,F2):力1(F1),力2(F2){//由两个力构成
}
力(F1、F2、F3、F3):力1(F1)、力2(F2)、力3(F3){//由三个力构成
}
点运算符()(双t){
回位力1(t)+回位力2(t);
}
点运算符()(双t){//此重载不应正确,因为它具有与上面相同的签名
返回力1(t)+力2(t)+力3(t);
}
};
//这是由主程序使用的
模板
力使物理(F1第一,F2第二){
返回力(第一、第二);
}
//这是由主程序使用的
模板
力使物理(F1第一,F2第二,F3第三){
返回力(第一、第二、第三);
}
您可以阅读(可变模板)
或者,您可以为模板提供如下默认参数:
template< typename F1, typename F2 = void, typename F3 = void>
模板
这两个都还没有在visual studio 2012中实现,但是。。。不知道你用什么
或者(你可能不喜欢)
复制您的类(不同的名称…)
并在1、2和3个参数以上分别设置模板…您可以阅读(可变模板)
或者,您可以为模板提供如下默认参数:
template< typename F1, typename F2 = void, typename F3 = void>
模板
这两个都还没有在visual studio 2012中实现,但是。。。不知道你用什么
或者(你可能不喜欢)
复制您的类(不同的名称…)
和模板以上的1、2和3个参数…可与以下参数一起使用:
模板
结构力
{
std::元组f;
};
您还可以提供不同的专门化(此方法在C++03中使用):
模板
结构力
{
F1 a;
F2 b;
F3 c;
};
模板
结构力
{
F1 a;
F2 b;
};
//等等。。。
您可以与以下工具一起使用:
模板
结构力
{
std::元组f;
};
您还可以提供不同的专门化(此方法在C++03中使用):
模板
结构力
{
F1 a;
F2 b;
F3 c;
};
模板
结构力
{
F1 a;
F2 b;
};
//等等。。。
为了简化演示,我将类型点
替换为双
。下面的代码应该足以说明我的观点:
// This will be the default F3 argument. It's basically a "zero force".
struct zero {
double operator()(double) { return 0.0; }
};
// You only need one struct Force
template< typename F1, typename F2, typename F3 = zero>
struct Force {
F1 force1;
F2 force2;
F3 force3;
Force(F1 f1, F2 f2, F3 f3 = zero()) : force1(f1), force2(f2), force3(f3) { // construct out of three forces
}
double operator()(double t) {
return force1(t) + force2(t) + force3(t);
}
};
// You might provide two make_physics overload for 2D and 3D problems (that's what you want, right?)
template< typename F1, typename F2>
Force<F1, F2> make_physics(F1 first, F2 second){
return Force<F1, F2>(first, second);
}
template< typename F1, typename F2, typename F3>
Force<F1, F2, F3> make_physics(F1 first, F2 second, F3 third){
return Force<F1, F2, F3>(first, second, third);
}
为了简化演示,我将类型Point
替换为double
。下面的代码应该足以说明我的观点:
// This will be the default F3 argument. It's basically a "zero force".
struct zero {
double operator()(double) { return 0.0; }
};
// You only need one struct Force
template< typename F1, typename F2, typename F3 = zero>
struct Force {
F1 force1;
F2 force2;
F3 force3;
Force(F1 f1, F2 f2, F3 f3 = zero()) : force1(f1), force2(f2), force3(f3) { // construct out of three forces
}
double operator()(double t) {
return force1(t) + force2(t) + force3(t);
}
};
// You might provide two make_physics overload for 2D and 3D problems (that's what you want, right?)
template< typename F1, typename F2>
Force<F1, F2> make_physics(F1 first, F2 second){
return Force<F1, F2>(first, second);
}
template< typename F1, typename F2, typename F3>
Force<F1, F2, F3> make_physics(F1 first, F2 second, F3 third){
return Force<F1, F2, F3>(first, second, third);
}
0
不是类型,它不编译0
不是类型名,不应编译。改用void
。另外,VS支持typename X=void
没有问题,因为至少VS2008…0
不是类型,它不编译0
不是类型名,不应该编译。改用void
。另外,VS支持typename X=void
,至少从VS2008开始就没有问题了。。。
template< typename F1, typename F2, typename... F3>
Force<F1, F2, F3...> make_physics(F1&& first, F2&& second, F3&&... third){
return Force<F1, F2, F3...>(std::forward<F1>(first), std::forward<F2>(second), std::forward<F3>(third)...);
}