zoukankan      html  css  js  c++  java
  • template template parameter

    测试代码

    C++ 11里面

    #include <stdexcept>
    #include <iostream>
    #include <memory>
    #include <vector>
    #include <string>
    #include <deque>
    
    template<typename T>
    using Vec = std::vector<T, std::allocator<T>>;
    
    class A
    {
    private:
        int a = 0;
        int b = 2;
    public:
        A() = default;
        A(int pa1, int pa2) :a(pa1), b(pa2) { printf("A Construct call!"); };
    };
    
    template<typename T,template<class> class Container>
    class XCls
    {
    private:
        Container<T> elem;
    public:
        XCls() {
            for (int i = 0; i < 4096; i++)
                elem.insert(elem.end(), T(i,4096-i));
        }
    };
    
    int main()
    {
        XCls<A, Vec> test1;
    }

    在C++ 14里面这样已经不会报错了

    #include <stdexcept>
    #include <iostream>
    #include <memory>
    #include <vector>
    #include <string>
    #include <deque>
    
    class A
    {
    private:
        int a = 0;
        int b = 2;
    public:
        A() = default;
        A(int pa1, int pa2) :a(pa1), b(pa2) { printf("A Construct call!"); };
    };
    
    template<typename T,template<class> class Container>
    class XCls
    {
    private:
        Container<T> elem;
    public:
        XCls() {
            for (int i = 0; i < 4096; i++)
                elem.insert(elem.end(), T(i,4096-i));
        }
    };
    
    int main()
    {
        XCls<A, std::vector> test1;
    }

    示例结果

  • 相关阅读:
    maven的安装和配置以及搭建项目应用
    Spring MVC与Struts2的区别(仅本人浅薄的理解)?
    记录学习PYTHON
    Zookeeper可以干什么
    Redis与Memcache的区别
    Redis持久化的两种方式和区别
    Scala 柯里化
    Redis与Memcached的区别
    高并发的处理策略
    序列化
  • 原文地址:https://www.cnblogs.com/pppyyyzzz/p/14361401.html
Copyright © 2011-2022 走看看