C++ 可变模板类-可变成员函数
我正在从事一个项目,该项目需要多个存储类的通用缓存。我的基类如下所示:C++ 可变模板类-可变成员函数,c++,templates,C++,Templates,我正在从事一个项目,该项目需要多个存储类的通用缓存。我的基类如下所示: template<typename ... Types> class VarCache{ template<typename T> using item_type = shared_ptr<holder<T>>; template<typename T> using map_type = map<int64_t,item_typ
template<typename ... Types>
class VarCache{
template<typename T>
using item_type = shared_ptr<holder<T>>;
template<typename T>
using map_type = map<int64_t,item_type <T>>;
public:
std::tuple<map_type<Types>...> caches;
};
这可能吗?可以使用一些技巧为参数包的每个元素调用函数。这里有一个选择:
void prune_all_variants() {
(void)std::initializer_list<int> {
(prune<Types>(), 0)...
};
}
void prune_all_variants(){
(无效)标准::初始值设定项列表{
(prune(),0)。。。
};
}
在C++17中,可以使用以下方法将其简化为:
void prune_all_variants(){
(prune(),…);
}
您可以使用:
void prune_all_variants()
{
int dummy[] = {0, (prune<Types>(), void(), 0)...};
static_cast<void>(dummy); // avoid warning for unused variable.
}
void prune_all_variants()
{
int dummy[]={0,(prune(),void(),0);
static_cast(dummy);//避免对未使用的变量发出警告。
}
或使用C++17中的折叠表达式:
void prune_all_variants() {
(static_cast<void>(prune<Types>()), ...);
}
void prune_all_variants(){
(static_cast(prune()),…);
}
void prune_all_variants()
{
int dummy[] = {0, (prune<Types>(), void(), 0)...};
static_cast<void>(dummy); // avoid warning for unused variable.
}
void prune_all_variants() {
(static_cast<void>(prune<Types>()), ...);
}