SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
sam
Topic Author
Posts: 703
Joined: December 5th, 2001, 12:04 pm

Significant difference? C++ Templates

April 14th, 2003, 10:15 pm

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
Last edited by sam on April 14th, 2003, 10:00 pm, edited 1 time in total.
 
User avatar
afoster
Posts: 314
Joined: July 14th, 2002, 3:00 am

Significant difference? C++ Templates

April 15th, 2003, 6:34 am

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)
 
User avatar
sam
Topic Author
Posts: 703
Joined: December 5th, 2001, 12:04 pm

Significant difference? C++ Templates

April 15th, 2003, 10:41 am

Thanks.
 
User avatar
Mukuzani
Posts: 137
Joined: March 12th, 2002, 3:59 am

Significant difference? C++ Templates

April 15th, 2003, 1:53 pm

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........................................
 
User avatar
sam
Topic Author
Posts: 703
Joined: December 5th, 2001, 12:04 pm

Significant difference? C++ Templates

May 4th, 2003, 7:21 pm

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
 
User avatar
mj
Posts: 3449
Joined: December 20th, 2001, 12:32 pm

Significant difference? C++ Templates

May 4th, 2003, 7:49 pm

the second template argument can only be a number in this case.MJ
 
User avatar
Patrik
Posts: 323
Joined: April 15th, 2002, 9:18 am

Significant difference? C++ Templates

May 4th, 2003, 8:55 pm

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.
 
User avatar
sam
Topic Author
Posts: 703
Joined: December 5th, 2001, 12:04 pm

Significant difference? C++ Templates

May 5th, 2003, 2:43 pm

Thanks for the responses. Much appreciated.Sam
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Significant difference? C++ Templates

January 29th, 2015, 11:32 am

From quantnet.com
Last edited by Cuchulainn on January 28th, 2015, 11:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
dd3
Posts: 246
Joined: June 8th, 2010, 9:02 am

Significant difference? C++ Templates

January 29th, 2015, 5:00 pm

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.
 
User avatar
Cuchulainn
Posts: 62591
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Significant difference? C++ Templates

January 30th, 2015, 10:39 am

Another example is traits and policy-based design
Last edited by Cuchulainn on January 29th, 2015, 11:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On