C++11的模板新特性-变长参数的模板

这个特性很赞,直接给例子吧,假如我要设计一个类,CachedFetcher内部可能使用std::map也可能使用std::unordered_map,也可能是其它的map,怎么设计呢?没有C++11变长模板之前这很费劲。。 因为map的模板参数可不是只有key,value两个啊,还有一些有默认参数的template参数。。。

template<typename _Key, typename _Value,

template class _Map = std::unordered_map>

class CachedFetcher

{

public:

typedef _Key Key;

typedef _Value Value;

typedef _Map Map;

typedef typename Map::iterator iterator;

typedef typename Map::const_iterator const_iterator;

private:

Map _map;

};

另外一个比较好的特性

template<typename _Key, typename _Value,

template class _Map = std::map>

class LruMap

{

};

template

using LruHashMap= LruMap;

最后非模板的一个有用特性,复用基类的构造函数

class FeatureVector : public Vector

{

public:

~FeatureVector() = default;

FeatureVector(FeatureVector&&) = default;

FeatureVector& operator = (FeatureVector&&) = default;

FeatureVector(const FeatureVector&) = default;

FeatureVector& operator = (const FeatureVector&) = default;

#ifndef GCCXML

using Vector::Vector;

#endif

}


原文链接: https://www.cnblogs.com/rocketfan/p/4081296.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/152744

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月11日 下午2:25
下一篇 2023年2月11日 下午2:42

相关推荐