模板适用于编程模板函数和类,因此我们可以使用缩短代码并让编译器为我们做一些工作.
在我的情况下,我想使用模板类,例如.
template <typename T, typename G> class unicorn {
T value01;
G value02; <- not used in ever instance of class unicorn
};
Run Code Online (Sandbox Code Playgroud)
有没有办法,编译器使用typename T = int创建一个实例,如果没有使用或指定,没有 typename G的版本?
所以结果如下:
unicorn <double, int>;
class unicorn {
double value01;
int value02;
};
Run Code Online (Sandbox Code Playgroud)
并且没有Argument或指定的typename G.
unicorn <double>
class unicorn {
T value01;
// "not included in this instance"
};
Run Code Online (Sandbox Code Playgroud)