如果不作限定,模板可以传入任意类型,这时我们可以为模板做参数类型限定,这样可以防止使用者传入了不支持的类型导致得到错误的结果。
template struct Checker; template <> struct Checker { typedef int Type; }; template <> struct Checker { typedef int Type; }; template void func(typename Checker::Type value) { int a = value; } int main() { func<int>(1); //因为参数限定为了int和short,传入float则会导致编译错误 func<float>(1.0f); return 0; }