Page 1 of 1

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

Posted: July 12th, 2012, 2:58 pm
by ludinski
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

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

Posted: July 17th, 2012, 2:41 pm
by SierpinskyJanitor
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

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

Posted: July 21st, 2012, 4:36 pm
by ludinski
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.

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

Posted: July 21st, 2012, 9:08 pm
by Cuchulainn
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.

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

Posted: July 22nd, 2012, 1:36 am
by ludinski
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?

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

Posted: July 22nd, 2012, 8:21 am
by Cuchulainn
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,...>))

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

Posted: July 22nd, 2012, 4:11 pm
by Polter
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):

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

Posted: July 23rd, 2012, 8:07 pm
by Cuchulainn
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.

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

Posted: July 23rd, 2012, 9:38 pm
by ludinski
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?

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

Posted: July 23rd, 2012, 9:52 pm
by ludinski
Could this Generic payoffs in C++ be implemented with variadics?

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

Posted: July 24th, 2012, 9:25 am
by Cuchulainn
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.

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

Posted: August 14th, 2012, 12:06 pm
by Cuchulainn
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.