简体   繁体   中英

Templates in c++,typename and class

What is the difference between writing a template in c++ in the below both ways

template<typename T> & template<class T>

and when do we prefer them above each other? If both are same,then why do we have them both if one is enough?

No difference at all. I prefer first one (mostly), but that is my personal taste. The language doesn't make any difference between them.

For template parameters, the keywords typename and class are equivalent. §14.1.2 says:

There is no semantic difference between class and typename in a template-parameter.


If both are same,then why do we have them both if one is enough?

Stan Lippman explains this in his article:

Why C++ Supports both Class and Typename for Type Parameters?

These keywords are there because of historical reason. There's no difference between them. Read this: http://blogs.msdn.com/b/slippman/archive/2004/08/11/212768.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM