
All, I have a question with regards more to the C++ than Boost. I would like to write a template which depending on the argument type does different things. For example the pseudo code: template <class T, class C> foo(T& nT, C& nC) { // common code; if(T==C) // specific code; ..... // again common code; } Can the template meta programming help me in this case? Sorry, I have no experice with this and I am just guessing. Do you have any suggestions? Thanks for help, Pshemek -------------------------------------------------------- If you are not an intended recipient of this e-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute it. Click here for important additional terms relating to this e-mail. http://www.ml.com/email_terms/ --------------------------------------------------------

Sliwa, Przemyslaw (London) wrote:
I would like to write a template which depending on the argument type does different things.
For example the pseudo code:
template <class T, class C> foo(T& nT, C& nC) { // common code; if(T==C) // specific code; .....
// again common code; }
Can the template meta programming help me in this case?
It may. It depends on whether "specific code" compiles when T!=C. If it does, you could do this: template <class T, class C> void foo(T& nT, C& nC) { // common code if(boost::is_same<T,C>::value) { /* specific code */ } // again common code } where is is_same<> is defined in boost/type_traits/is_same.hpp. If "specific code" doesn't compile when T!=C, you need to get fancier. An overloaded helper function is the way to go: template<class T, class C> void specific_code(T&, C&) {} template<class T> void specific_code(T&, T&) { // specific code here } template <class T, class C> void foo(T& nT, C& nC) { // common code specific_code(nT, nC); // again common code } HTH, -- Eric Niebler Boost Consulting www.boost-consulting.com
participants (2)
-
Eric Niebler
-
Sliwa, Przemyslaw (London)