c++ - How to replace macro with a template function? -


i have macro:

#define two_cmd( c1, c2 ) { const long r1=c1; if ( r1 ) return r1; return c2; } 

and using:

long myclass::somefunc( long ) {     //...     if ( )         two_cmd( func<int>(a), func<void>() );     else         two_cmd( func<double>(), func<std::string>(a) );     //... } 

func template member functions. key requirement keep readability of code!

i guess there variant template member function have pointer member functions arguments:

return two_cmd( func<int>, a, func<void> ); 

but syntax not clear.

first thing first: hiding return statement inside of macro evil. when 1 looks @ function, not @ clear calls two_cmd cause function return.

the easiest way pass callable objects function template , have return result:

template <typename r, typename f, typename g> r evaluate(const f& f, const g& g) {     r x = f();     return x ? x : g(); } 

used as:

return evaluate<long>(     std::bind(&myclass::func<int>, this, a),      std::bind(&myclass::func<void>, this));  return evaluate<long>(     std::bind(&myclass::func<double>, this),      std::bind(&myclass::func<std::string>, this, a)); 

if compiler , standard library not support c++0x or c++ tr1 bind, there implementation in boost identical.

(i've named function evaluate because can't think of name function.)


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -