是否可以有一个带有可选模板参数的类,可以像这样调用?:
#include <iostream>
template <typename T = void>
class A final
{
public:
// This class can be called only when T exists.
void f()
{
printf("%d\n", member);
}
// This method can be called only when T is missing.
void g()
{
printf("No template parameter\n");
}
public:
T member;
};
int main()
{
A<int> a1;
A a2;
a1.f(); // should be valid
a1.g(); // should be invalid, cannot compile
a2.f(); // should be invalid, cannot compile
a2.g(); // should be valid
return 0;
}
如果是,应该使用哪些 std 函数?