【发布时间】:2016-03-01 21:06:56
【问题描述】:
以一种非常简单的方式。我们有以下代码:
struct templateTest{
struct {}m_none;
template <typename T1, typename T2, typename T3, typename T4>
templateTest(T1 arg1, T2 arg2=m_none, T3 arg3=m_none, T4 arg4=m_none){
}
};
struct nonTemplate{
nonTemplate(int arg1, int arg2=2, int arg3=3){}
};
int main()
{
nonTemplate(5);
nonTemplate(5,10);
templateTest test2(5,10);
templateTest test1(5);
return 0;
}
我想要的是模板类中的默认构造函数参数。
默认参数在nonTemplate 类中工作得很好,但在templateTest 类型中却不是这样。我遇到的错误如下:
C2660: 'templateTest::templateTest' : 函数不接受 2 个参数
用于使用 2 个参数调用的构造函数
对于使用一个参数调用的构造函数,编译器想要调用 copyConstructor 并且输出如下:
C2664: 'templateTest::templateTest(const templateTest &)' : 无法将参数 1 从 'int' 转换为 'const templateTest &'
有什么方法可以在旧的 C++ 编译器上实现我想要的,比如 msvc 2011?
【问题讨论】:
标签: c++ templates default-value