C++ 模板类表达式参数重载
嘿,我想弄清楚是否有可能用表达式参数“重载”模板类定义。有点像下面的代码片段C++ 模板类表达式参数重载,c++,templates,parameters,overloading,C++,Templates,Parameters,Overloading,嘿,我想弄清楚是否有可能用表达式参数“重载”模板类定义。有点像下面的代码片段 template<class T> class Test { public: T testvar; Test() { testvar = 5; cout << "Testvar: " << testvar << endl; } }; template<class T> class Test&l
template<class T>
class Test
{
public:
T testvar;
Test()
{
testvar = 5;
cout << "Testvar: " << testvar << endl;
}
};
template<class T>
class Test<T, int num>
{
public:
T testvar;
Test()
{
testvar = 10;
cout << "Testvar: " << testvar << endl;
cout << "Param: " << num << endl;
}
};
模板
课堂测试
{
公众:
T-testvar;
测试()
{
testvar=5;
cout模板允许默认的模板参数,它可以提供类似于您所寻找的东西
template<class T, int num = -1>
class Test
{
public:
T testvar;
Test()
{
testvar = (num == -1 ? 10 : 5);
cout << "Testvar: " << testvar << endl;
if ( num != -1 )
cout << "Param: " << num << endl;
}
};
模板
课堂测试
{
公众:
T-testvar;
测试()
{
testvar=(num==-1?10:5);
cout模板允许默认的模板参数,它可以提供类似于您所寻找的东西
template<class T, int num = -1>
class Test
{
public:
T testvar;
Test()
{
testvar = (num == -1 ? 10 : 5);
cout << "Testvar: " << testvar << endl;
if ( num != -1 )
cout << "Param: " << num << endl;
}
};
模板
课堂测试
{
公众:
T-testvar;
测试()
{
testvar=(num==-1?10:5);
cout如果您希望能够为测试
仅指定一个模板参数,则需要将默认模板参数声明为
也可以部分专门用于不同的参数值:
// This base template will be used whenever the second parameter is
// supplied and is not -1.
template<class T, int num = -1>
class Test
{
public:
T testvar;
Test()
{
testvar = 10;
cout << "Testvar: " << testvar << endl;
cout << "Param: " << num << endl;
}
};
// This partial specialisation will be chosen
// when the second parameter is omitted (or is supplied as -1).
template<class T, int num>
class Test<T, -1>
{
public:
T testvar;
Test()
{
testvar = 5;
cout << "Testvar: " << testvar << endl;
}
};
//只要第二个参数是
//已提供,但不是-1。
模板
课堂测试
{
公众:
T-testvar;
测试()
{
testvar=10;
cout如果您希望能够为测试
仅指定一个模板参数,则需要将默认模板参数声明为
也可以部分专门用于不同的参数值:
// This base template will be used whenever the second parameter is
// supplied and is not -1.
template<class T, int num = -1>
class Test
{
public:
T testvar;
Test()
{
testvar = 10;
cout << "Testvar: " << testvar << endl;
cout << "Param: " << num << endl;
}
};
// This partial specialisation will be chosen
// when the second parameter is omitted (or is supplied as -1).
template<class T, int num>
class Test<T, -1>
{
public:
T testvar;
Test()
{
testvar = 5;
cout << "Testvar: " << testvar << endl;
}
};
//只要第二个参数是
//已提供,但不是-1。
模板
课堂测试
{
公众:
T-testvar;
测试()
{
testvar=10;
谢谢,这就是我要找的。谢谢,这就是我要找的。