templates - Ways to generalize C++ code with different similar types -


currently have following code:

static void markpoolsfree(const tnetgrouppools &group_info, tobjectid netiface) {     (size_t = 0; i<group_info.public_pools.length();i ++ ) {         sdk::revokeippoolfromnetworkinterface(group_info.public_pools[i],netiface);     }      (size_t =0 ; i<group_info.private_pool.length(); i++) {         sdk::revokeippoolfromnetworkinterface(group_info.private_pool[i].pool_id,                                               netiface);     } } 

which has same logic, differents in types group_info.public_pools[i] , group_info.private_pool[i], that's why in second loop have add .pool_id member call. these types different , don't have relationships.

i want rewrite code make more general, example (sketch):

// template function template <typename container, typename predargs> static void revokeippool(const container &pool, tobjectid netiface,                          bool (*pred)(predargs pool_id, predargs netiface_id)) {      (size_t = 0; < pool.length(); ++i) {          if (pred(pool[i], netiface)) {              sdk::revokeippoolfromnetworkinterface(pool[i], netiface);          }       } }  // calling code static void markpoolsfree(const tnetgrouppools &group_info, tobjectid netiface) {     revokeippool(group_info.public_pools, netiface, sdk::ispredicatetrue);     revokeippool(group_info.private_pool, netiface, sdk::ispredicatetrue); } 

but problem in different types public_pools , private_pool.

question: give ways how possible generalize code examples? need c++03 code, c++11/c++14 acceptable.

my thoughts:

  1. instead of sdk::revokeippoolfromnetworkinterface make wrapperippool overloads both types , call sdk::revokeippoolfromnetworkinterface internally.
  2. overload revokeippool both types (but it's code duplication, no improvement against original code)
  3. partial function template specialization revokeippool (is possible?)
  4. full function template specialization revokeippool
  5. wrap function revokeippool in class , make partial class template specialization.

questions:

  1. wich of thoughts correct?
  2. what's advantages , shortcomings?
  3. which more idiomatic c++03 or c++11?
  4. is there other solutions?

in c++03 i'd use template specialization on traits class. (the main reason going through traits class, rather using direct function specialisation, or overload can perform partial template specialisation classes, not functions - , while not needed here, may useful later )

static void markpoolsfree(const tnetgrouppools &group_info, tobjectid netiface) {     markpoolsfreeimpl(group_info.public_pools, netiface);     markpoolsfreeimpl(group_info.private_pools, netiface); }  template<typename t> static void markpoolsfreeimpl(pools pools, tobjectid netiface) {     (size_t = 0; i<pools.length();i ++ ) {        poolid poolid = poollisttrait<pools>::get(pools,i);        sdk::revokeippoolfromnetworkinterface(poolid,netiface);     } }  template<typename t> class poollisttrait {};  template<> class poollisttrait<publicpoollist> {   static poolid get(publicpoollist pools, int i) { return pools[i]; } }  template<> class poollisttrait<privatepoollist> {   static poolid get(privatepoollist pools, int i) { return pools[i].pool_id; } } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -