SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
ludinski
Topic Author
Posts: 63
Joined: January 9th, 2009, 8:54 am

Is it possible to implement the Strategy pattern using variadic templates?

July 12th, 2012, 2:58 pm

Hi,I'm learning C++11 and trying to find practical use of variadic templates besides the type safe printf example usually given in tutorials. Can it be used to implement a form of Strategy pattern taking a variable numbers of parameters?Thanks
 
User avatar
SierpinskyJanitor
Posts: 1069
Joined: March 29th, 2005, 12:55 pm

Is it possible to implement the Strategy pattern using variadic templates?

July 17th, 2012, 2:41 pm

Howdy,Mr. ludinsky are you learning C++ or already know C++ and are just brushing up on the C++11 version? That's a rather advanced topic to begin with, which should only be tackled once generic programming and template metaprogramming techniques have been mastered ( assuming that's C++ you're interested in and not just the concepts - Josuttis, TICPPVol2 first perhaps )? Also, if you're planning on studying this in order to use it in any bank/hf, then just learn the topics/theory, most of these techniques are already commonplace with scripting languages.best regards,Serp
 
User avatar
ludinski
Topic Author
Posts: 63
Joined: January 9th, 2009, 8:54 am

Is it possible to implement the Strategy pattern using variadic templates?

July 21st, 2012, 4:36 pm

QuoteOriginally posted by: SierpinskyJanitorHowdy,Mr. ludinsky are you learning C++ or already know C++ and are just brushing up on the C++11 version? That's a rather advanced topic to begin with, which should only be tackled once generic programming and template metaprogramming techniques have been mastered ( assuming that's C++ you're interested in and not just the concepts - Josuttis, TICPPVol2 first perhaps )? Also, if you're planning on studying this in order to use it in any bank/hf, then just learn the topics/theory, most of these techniques are already commonplace with scripting languages.best regards,SerpMr SierpinskiJanitor please have a look at stackoverflow. But you're right I m not sure this is very useful.
Last edited by ludinski on July 20th, 2012, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 62921
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Is it possible to implement the Strategy pattern using variadic templates?

July 21st, 2012, 9:08 pm

I have not looked at variadics but if no compelling reason can be found, then it becomes a solution loooking for a problem. The 20/80 rule still holds for C+. Someone should have an 'eye-opener' solution.
Last edited by Cuchulainn on July 20th, 2012, 10: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
ludinski
Topic Author
Posts: 63
Joined: January 9th, 2009, 8:54 am

Is it possible to implement the Strategy pattern using variadic templates?

July 22nd, 2012, 1:36 am

QuoteOriginally posted by: CuchulainnI have not looked at variadics but if no compelling reason can be found, then it becomes a solution loooking for a problem. The 20/80 rule still holds for C+. Someone should have an 'eye-opener' solution.It seems that according to the following link variadic templates facilitates policy based design (i.e. compile time strategy pattern) post modern c++I m just trying to figure out how?
 
User avatar
Cuchulainn
Posts: 62921
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Is it possible to implement the Strategy pattern using variadic templates?

July 22nd, 2012, 8:21 am

QuoteOriginally posted by: ludinskiQuoteOriginally posted by: CuchulainnI have not looked at variadics but if no compelling reason can be found, then it becomes a solution loooking for a problem. The 20/80 rule still holds for C+. Someone should have an 'eye-opener' solution.It seems that according to the following link variadic templates facilitates policy based design (i.e. compile time strategy pattern) post modern c++I m just trying to figure out how?The idea of policy-based design in C++ is good but the underling concepts have been know for > 25 years and in a more general context (ADL, MIL, plug and socket arch). PBD is a realisation.....The variadics might help in creating a universal print algo/strategy? Here is for sequential containers using <<template-template>> but could variadics do it for all containers (e.g. map<K,V,...>))
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Polter
Posts: 2526
Joined: April 29th, 2008, 4:55 pm

Is it possible to implement the Strategy pattern using variadic templates?

July 22nd, 2012, 4:11 pm

QuoteOriginally posted by: Cuchulainntemplate <typename T, template <typename S,typename Alloc > class Container,typename TAlloc> void print(const Container<T, TAlloc>& container, const std::string& comment){ // A generic print function for sequential containers std::cout << comment << ": "; std::for_each(container.begin(),container.end(), [] (const T& t) { std::cout << t << ", ";}); std::cout << endl;}Why not something like this -- seems simpler and more generic (without needing variadics):
Last edited by Polter on July 21st, 2012, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 62921
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Is it possible to implement the Strategy pattern using variadic templates?

July 23rd, 2012, 8:07 pm

Sure. In this case 'Container' is an amorphous structure and in more complex cases we may wish to model its template parameters explicitly. e.g. a device that talks to both sensors and actuators.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
ludinski
Topic Author
Posts: 63
Joined: January 9th, 2009, 8:54 am

Is it possible to implement the Strategy pattern using variadic templates?

July 23rd, 2012, 9:38 pm

QuoteOriginally posted by: PolterQuoteOriginally posted by: Cuchulainntemplate <typename T, template <typename S,typename Alloc > class Container,typename TAlloc> void print(const Container<T, TAlloc>& container, const std::string& comment){ // A generic print function for sequential containers std::cout << comment << ": "; std::for_each(container.begin(),container.end(), [] (const T& t) { std::cout << t << ", ";}); std::cout << endl;}Why not something like this -- seems simpler and more generic (without needing variadics):template <typename Container>void print(const Container & container, const std::string & comment){ std::cout << comment << ": "; for (const auto & element : container) std::cout << element << ", "; std::cout << std::endl;}I think we're missing the point here. Variadic templates is about a template with an arbitrary number of template parameters. Putting the strategy pattern aside, could it be used for example to specify payoffs in a completely generic and type safe way? Could this Generic payoffs in C++ be implemented with variadics?
Last edited by ludinski on July 22nd, 2012, 10:00 pm, edited 1 time in total.
 
User avatar
ludinski
Topic Author
Posts: 63
Joined: January 9th, 2009, 8:54 am

Is it possible to implement the Strategy pattern using variadic templates?

July 23rd, 2012, 9:52 pm

Could this Generic payoffs in C++ be implemented with variadics?
Last edited by ludinski on July 22nd, 2012, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 62921
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Is it possible to implement the Strategy pattern using variadic templates?

July 24th, 2012, 9:25 am

QuoteOriginally posted by: ludinskiCould this Generic payoffs in C++ be implemented with variadics?Maybe. In the sense that VT are approximating functional programming's recursion? But payoff language is declarative, not imperative? It feels kind of artificial.
Last edited by Cuchulainn on July 23rd, 2012, 10: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
Cuchulainn
Posts: 62921
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Is it possible to implement the Strategy pattern using variadic templates?

August 14th, 2012, 12:06 pm

Maybe VT are for library builders instead of having <T1=aa, ...., T10 == zz><T1=aa, ...., T9== zz>Now a uniform syntax. ...Strategy uses parameter and varying number at that, so you could have algo with a variadic Tuple as input parameter.
Last edited by Cuchulainn on August 13th, 2012, 10: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