zoukankan      html  css  js  c++  java
  • C++11的模板新特性-变长参数的模板

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

       

    template<typename _Key, typename _Value,

    template<class _Kty, class _Ty, typename...> class _Map = std::unordered_map>

    class CachedFetcher

    {

    public:

    typedef _Key Key;

    typedef _Value Value;

    typedef _Map<Key, Value> Map;

    typedef typename Map::iterator iterator;

    typedef typename Map::const_iterator const_iterator;

       

    private:

    Map _map;

    };

       

       

       

    另外一个比较好的特性

    template<typename _Key, typename _Value,

    template<class _Kty, class _Ty, typename...> class _Map = std::map>

    class LruMap

    {

    };

    template <typename Key, typename Value, typename...>

    using LruHashMap = LruMap<Key, Value, std::unordered_map>;

       

       

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

       

    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

    }

       

  • 相关阅读:
    git学习02
    每日一记8.12
    git学习01
    每日一记8.7
    每日一记8.6
    spring boot使用tomcat启动
    每日一记8.1
    【学习笔记】HTML5 WebGL游戏引擎开发
    【转】使用 WebGL 进行 3D 开发,第 3 部分: 添加用户交互
    【转】使用 WebGL 进行 3D 开发,第 2 部分: 使用 WebGL 库以更少的编码做更多的事情
  • 原文地址:https://www.cnblogs.com/rocketfan/p/4081296.html
Copyright © 2011-2022 走看看