Page 1 of 1

Significant difference? C++ Templates

Posted: April 14th, 2003, 10:15 pm
by sam
Hi all, Just a query that I've come up with regarding the declaration of Templates.... What is the difference (is there?) between the following template declarations?template<class T> class Test{...};template<typename T>class Test{...};As far as I can tell from some simple programs the only difference is that the first is used for the standard types used in C++, e.g. int, double, char... and the latter is used for user specified classes that C++ would not know about normally. But is there anything else subtle happening here?Many Thanks!Sam

Significant difference? C++ Templates

Posted: April 15th, 2003, 6:34 am
by afoster
The typename and class keywords are completely interchangable and the preference to use one or the other is simply a matter of style (see Bjarne Stroustrup The C++ Programming Language, 3rd Edition)

Significant difference? C++ Templates

Posted: April 15th, 2003, 10:41 am
by sam
Thanks.

Significant difference? C++ Templates

Posted: April 15th, 2003, 1:53 pm
by Mukuzani
QuoteOriginally posted by: afosterThe typename and class keywords are completely interchangable and the preference to use one or the other is simply a matter of style (see Bjarne Stroustrup The C++ Programming Language, 3rd Edition)They are the same in sam's example, but could be different in different context. Scott Meyers in "effective STL" gives an example of dependent types (p. 8)template<typename C>bool f(C& c){typename C::iterator it;//should be typename........................................

Significant difference? C++ Templates

Posted: May 4th, 2003, 7:21 pm
by sam
I've recently come across the following definition for a template:template<typename T, size_t size>I'm ok with the first term in the brackets, but have never seen the second term's declaration type. Is it some special version of 'typename' or 'class' that only allows numerical objects?Thanks,Sam

Significant difference? C++ Templates

Posted: May 4th, 2003, 7:49 pm
by mj
the second template argument can only be a number in this case.MJ

Significant difference? C++ Templates

Posted: May 4th, 2003, 8:55 pm
by Patrik
All the OS'es I have looked at has size_t as a typedef to an integer type of some kind/size.You can safely look at it as a integer until you really need to know more - then consult theheaderfiles for your OS/compiler.

Significant difference? C++ Templates

Posted: May 5th, 2003, 2:43 pm
by sam
Thanks for the responses. Much appreciated.Sam

Significant difference? C++ Templates

Posted: January 29th, 2015, 11:32 am
by Cuchulainn
From quantnet.com

Significant difference? C++ Templates

Posted: January 29th, 2015, 5:00 pm
by dd3
QuoteOriginally posted by: samI've recently come across the following definition for a template:template<typename T, size_t size>I'm ok with the first term in the brackets, but have never seen the second term's declaration type. Is it some special version of 'typename' or 'class' that only allows numerical objects?Thanks,SamThe second template parameter is used to let the programmer specify a compile-time constant.The most obvious use of this would be for std::array<T, N> where N is the number of elements in the array.

Significant difference? C++ Templates

Posted: January 30th, 2015, 10:39 am
by Cuchulainn
Another example is traits and policy-based design