【240818】C++ 泛型编程里,为什么都用 typename T,而不是 class T?

2 阅读1分钟

参考资料

  1. 《Programming -- Principles and Practice Using C++ (3rd Edition)》—— Bjarne Stroustrup

C++ 泛型编程里,为什么都用 typename T,而不是 class T?

Instead of writing template<typename T>, you can write template<class T>. The two constructs mean exactly the same thing, but some prefer typename ‘‘because it is clearer’’ and ‘‘because nobody gets confused by typename thinking that you can’t use a built-in type, such as int, as a template argument.’’ We are of the opinion that class already means type, so it makes no difference. Also, class is shorter.