C++ 11/14 doing a different operation in a single function based on template type

ساخت وبلاگ

Vote count: 0

So i've got a bunch of shared_ptr's each pointing to a different template Cache classes. Like so:

shared_ptr< Cache > t1Cache;
shared_ptr< Cache > t2Cache;
shared_ptr< Cache > t3Cache;

Each Cache class simply holds a map with a string as a key and T as the item. I want to have a single function for an update,add and get operations. So I could call for example:

 auto something = getItem( key1, TYPE_T1 ); addItem( key2, TYPE_T1, newItem ); updateItem( key3, TYPE_t1, newValue);

I tried at first just having a switch based on the enum like so:

template auto addItem( const std::string& key, ITEM_TYPE type, T nItem )
{ switch( type ) { case TYPE_T1: t1Cache->addItem( key, nItem ); break; case TYPE_T2: t2Cache->addItem( key ,nItem ); break; }
}

Unfortunately I have a basic knowledge of templates so I didn't realize that that will not work. At the moment as a temporary solution im just using a different function for each type, though i'd really like to consolidate it all into one. I thought that maybe 'constexpr if' will help but it's not supported, is this even possible?

asked 33 secs ago

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : نویسنده : استخدام کار backsoft بازدید : 260 تاريخ : جمعه 25 فروردين 1396 ساعت: 1:03